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

新手上路,请多包涵

当我关注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.

我的环境如下:

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

原文由 user10945377 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 426
1 个回答

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

根据需要设置 --- 或设置 GOOGLE_APPLICATION_CREDENTIALS FIREBASE_CONFIG 环境变量,然后运行 gcloud auth application-default login ;那么你可以使用 admin.credential.applicationDefault() 而不是 admin.credential.cert(serviceAccount)

原文由 Martin Zeitler 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏