gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:04:16 24 4
gpt4 key购买 nike

当我按照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
    }
  • 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

    最佳答案

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

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

    刚打
     firebase deploy --only functions

    在 Node v8 中。

    关于node.js - 警告,缺少 FIREBASE_CONFIG 环境变量。初始化 firebase-admin 将失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54292448/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com