NodeJS 警告,缺少FIREBASE_CONFIG环境变量初始化firebase-admin将失败

xwmevbvl  于 2022-12-03  发布在  Node.js
关注(0)|答案(9)|浏览(117)

当我按照firebase的文件,但我无法连接到firebase。
有人面临这个问题吗?

我的代码:

  • index.js
var admin = require("firebase-admin");
var serviceAccount = require("./key/serviceAccountKey.json");
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://sampleLink.firebaseio.com",
});
const functions = require("firebase-functions");

var db = admin.firestore();
var data = {
    name: "Los Angeles",
    state: "CA",
    country: "USA",
};

// Add a new document in collection "cities" with ID 'LA'
var setDoc = db.collection("cities").doc("LA").set(data);
setDoc;
  • package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
},
"dependencies": {
    "firebase": "^5.7.3",
    "firebase-admin": "~6.0.0",
    "firebase-functions": "^2.1.0"
},
"private": true
}
  • console
tktktk:functions dev$ node index.js
Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail

The behavior for Date objects stored in Firestore is going to change
AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to add the
following code to your app before calling any other Cloud Firestore methods:

const firestore = new Firestore();
const settings = {/* your settings... */ timestampsInSnapshots: true};
firestore.settings(settings);

With this change, timestamps stored in Cloud Firestore will be read back as
Firebase Timestamp objects instead of as system Date objects. So you will also
need to update code expecting a Date to instead expect a Timestamp. For example:

// Old:
const date = snapshot.get('created_at');
// New:
const timestamp = snapshot.get('created_at');
const date = timestamp.toDate();

Please audit all existing usages of Date when you enable the new behavior. In a
future release, the behavior will change to the new behavior, so if you do not
follow these steps, YOUR APP MAY BREAK.

我的env如下:

$ firebase --version
6.3.0
$ node -v
v8.12.0
$ npm -v
6.4.1
i2byvkas

i2byvkas1#

FIREBASE_CONFIG警告提示JSON的路径丢失(或错误)。
根据需要设置FIREBASE_CONFIG,或者将GOOGLE_APPLICATION_CREDENTIALS设置为environment variable,然后运行gcloud auth application-default login;则可以使用admin.credential.applicationDefault()而不是admin.credential.cert(serviceAccount)

8dtrkrch

8dtrkrch2#

在本地运行云函数的正确方法是通过Functions shell(参见https://firebase.google.com/docs/functions/local-emulator)。如果你想像$ node index.js一样直接运行它,那么你必须自己设置所需的环境变量,否则firebase-functions会抱怨。
但是,请注意,上面的消息只是一个警告。尽管如此,您的代码仍然在运行。如果您没有看到任何数据写入Firestore,那可能是因为您没有处理Firestore set()方法返回的promise。

vaj7vani

vaj7vani3#

对我来说,问题在于那句台词:
const functions = require('firebase-functions');
我刚刚从 * index.js * 中删除了它。我不再使用Firebase Cloud函数了,但是我忘记删除那一行了。这就是我的问题所在。
因此,也许你也导入了这个包在一个文件中,不需要它。

6ie5vjzr

6ie5vjzr4#

我遇到了完全相同的问题。Turns out it has to do with Node version 10我删除了节点10,回到节点8,一切都像一个魅力一样工作...

const functions = require('firebase-functions');
 const admin = require('firebase-admin');
 admin.initializeApp();

就打

firebase deploy --only functions

在节点v8中。

wrrgggsh

wrrgggsh5#

关于你的问题,刚才感谢你的代码,我能够解决我的问题.
我做什么:

const admin = require('firebase-admin');

var serviceAccount = require("./serviceAccount.json");

admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });

const db = admin.firestore();

我使用的是Cloud Firestore数据库,而不是真实的数据库,所以我不发送initializeApp中的第二个参数,我只是将数据库的配置放在环境文件中。

export const environment = {
  production: false,
  firebaseConfig : {
    apiKey: 'XXXXXXXXXXX',
    authDomain: 'XXXXXXXXXX',
    databaseURL: 'XXXXXXXXXX',
    projectId: 'XXXXXXXXX',
    storageBucket: 'XXXXXXXXXXX',
    messagingSenderId: 'XXXXXXXXXXX',
    appId: 'XXXXXXXXXXXX',
    measurementId: 'XXXXXXX'
  }
};
093gszye

093gszye6#

这对我很有效:

admin.initializeApp({
  credential: admin.credential.cert({
      "type": "service_account",
      "project_id": "took from Firebase Generated Private Key",
      "private_key_id": "took from Firebase Generated Private Key",
      "private_key": "took from Firebase Generated Private Key",
      "client_email": "took from Firebase Generated Private Key",
      "client_id": "took from Firebase Generated Private Key",
      "auth_uri": "took from Firebase Generated Private Key",
      "token_uri": "took from Firebase Generated Private Key",
      "auth_provider_x509_cert_url": "took from Firebase Generated Private Key",
      "client_x509_cert_url": "took from Firebase Generated Private Key"
  }),
  databaseURL: "https://test-dia.firebaseio.com"
});
lstz6jyr

lstz6jyr7#

从2021年起,删除警告的正确方法是将firebase-functions-test添加到dev-dependencies中,并在jest配置中调用它,如下所示:

// jest.preset.js
const test = require('firebase-functions-test')();
// ...

请在此处阅读有关“脱机模式”测试的更多信息:https://firebase.google.com/docs/functions/unit-testing#offline-mode

vm0i2vca

vm0i2vca8#

如果您尝试在本地运行云功能,并希望使用云身份验证和数据库,例如
步骤:1 -转到Firebase控制台,将凭据数据复制为JSON格式,例如:

{
      "apiKey": "<YOUR-API-KEY>",
      "authDomain": "<YOUR-authDomain>",
      "databaseURL": "<YOUR-databaseURL>",
      "projectId": "...",
      "storageBucket": "...",
      "messagingSenderId": "...",
      "appId": "..."
}

2 -在您项目文件夹中,将其保存为JSON文件例如'my_credentials.json'
3 -在本地运行函数:(注意猫)
$ FIREBASE_CONFIG=$(cat my_credentials.json)npm运行本地
注意(!):将“local”脚本添加到package.json文件:

"scripts": {
   ...
   "local": "npx functions-framework --target=<YOUR-APP-NAME> --signature-type=<YOUR-FUNCTION-CALL-METHOD-EXAMPLE-http>"
 },
enxuqcxy

enxuqcxy9#

尝试将此版本依赖关系(在package.json中)与节点v8一起使用。

"dependencies": {
    "firebase-admin": "~7.1.1",
    "firebase-functions": "^2.2.1"
}
  • 这对我很有效 *

相关问题