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

时间:2019-01-21 14:50:19

标签: node.js firebase google-cloud-firestore

当我遵循firebase的文档,但无法连接到firebase时。
有人面对这个问题吗?
https://firebase.google.com/docs/firestore/quickstart https://firebase.google.com/docs/firestore/manage-data/add-data

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
}

控制台返回

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.

我的环境在下面:
$ firebase --version
6.3.0
$节点-v
v8.12.0
$ npm -v
6.4.1

6 个答案:

答案 0 :(得分:3)

我遇到了完全相同的问题。 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中。

答案 1 :(得分:1)

在本地运行云功能的正确方法是通过“功能”外壳程序(请参阅https://firebase.google.com/docs/functions/local-emulator)。如果您想像$ node index.js一样直接运行它,则必须自己设置所需的环境变量,否则firebase-functions会抱怨。

但是,请注意,以上消息仅是警告。尽管如此,您的代码仍在运行。如果您没有看到任何写入Firestore的数据,那可能是因为您没有处理Firestore set()方法返回的承诺。

答案 2 :(得分:1)

FIREBASE_CONFIG警告提示JSON的路径丢失(或错误)。

根据需要设置FIREBASE_CONFIG-或将GOOGLE_APPLICATION_CREDENTIALS设置为environment variable,然后运行gcloud auth application-default login;那么您可以使用admin.credential.applicationDefault()代替admin.credential.cert(serviceAccount)

答案 3 :(得分:0)

尝试在节点v8中使用此版本依赖项(在package.json中)。它对我有用。

"dependencies": {
"firebase-admin": "~7.1.1",
"firebase-functions": "^2.2.1"

},

答案 4 :(得分:0)

关于您的问题,刚才,由于您的代码,我得以解决我的问题。

我做什么:

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'
  }
};

答案 5 :(得分:-1)

对我来说,问题在于那条线:

7.0

我刚刚从 index.js 中删除了它。我不再使用Firebase Cloud Function,但是我忘记删除该行。 这是我的问题所在。

所以也许您也将此软件包导入了不需要的文件中。

相关问题