当我使用 dotenv 文件创建 Firebase 管理员凭据时收到此错误。我想要隐藏 Firebase 凭据并想要解析私钥 JSON。请专家看看
错误
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
undefined:1
undefined
^
SyntaxError: "undefined" is not valid JSON
at JSON.parse (<anonymous>)
at Object.<anonymous> (K:\Development\MindFuel\Backend\Helpers\database\Firebase\config.js:1:29)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Module.require (node:internal/modules/cjs/loader:1235:19)
at require (node:internal/modules/helpers:176:18)
at Object.<anonymous> (K:\Development\MindFuel\Backend\Helpers\database\Firebase\connectFirebase.js:3:22)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
Node.js v20.11.1
[nodemon] app crashed - waiting for file changes before starting...
DotEnv 文件
#Firebase
SERVICE_ACCOUNT='{
"private_key": "-----BEGIN PRIVATE KEY-----\---------\n-----END PRIVATE KEY-----\n",
"type":"service_account",
"project_id":"---" ,
"private_key_id": "-----",
"client_email": "------",
"client_id": "----",
"auth_uri": "--------",
"token_uri": "-------",
"auth_provider_x509_cert_url": "-------",
"client_x509_cert_url": "-------",
"universe_domain": "--------"
}'
Config.js
const serviceAccount = JSON.parse(process.env.SERVICE_ACCOUNT);
module.exports = serviceAccount;
ConnectFirebase.js
var admin = require("firebase-admin");
var serviceAccount = require('./config');
const firebaseDb = () => {
console.log(serviceAccount);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: `gs://${process.env.FIREBASE_PROJECT_ID}.appspot.com`
})
console.log("firebase Connected");
};
module.exports = firebaseDb;
任何帮助将不胜感激:)
嘿,我遇到了完全相同的问题,你解决了吗?