【问题标题】:Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail警告,FIREBASE_CONFIG 环境变量丢失。初始化 firebase-admin 将失败
【发布时间】:2019-06-14 23:44:53
【问题描述】:

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

【问题讨论】:

  • 要摆脱那个冗长的警告,请添加一行firebase.firestore().settings({timestampsInSnapshots: true});。见stackoverflow.com/questions/51479679/…。我不确定关于FIREBASE_CONFIG 的警告。 “我无法连接到 Firebase”是什么意思?
  • 嗨弗兰克,谢谢你的建议。它可以消除冗长的警告。但是由于缺少 FIREBASE_CONFIG,我仍然无法保存数据。
  • 我面临这个问题一个多月了,仍然无法做任何 hello world 的事情。

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 提问者说他​​们使用的是 Node v8.12.0
    【解决方案3】:

    在本地运行云函数的正确方法是通过 Functions shell(请参阅https://firebase.google.com/docs/functions/local-emulator)。如果你想像$ node index.js一样直接运行,那么你必须自己设置所需的环境变量,否则firebase-functions会报错。

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

    【讨论】:

    【解决方案4】:

    对我来说,问题在于那一行:

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

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

    所以也许你也将这个包导入到一个不需要它的文件中。

    【讨论】:

    • 这对我来说也是一个问题。
    【解决方案5】:

    关于你的问题,刚才感谢你的代码,我能够解决我的问题。

    我做什么:

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

    【讨论】:

      【解决方案6】:

      这对我有用:

      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"
      });

      【讨论】:

        【解决方案7】:

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

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

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

        【讨论】:

          【解决方案8】:

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

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

          它对我有用。

          【讨论】:

            猜你喜欢
            • 2019-09-29
            • 2019-11-08
            • 1970-01-01
            • 1970-01-01
            • 2018-05-23
            • 2015-08-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多