簡體   English   中英

當我嘗試初始化 firebase 應用程序時,為什么 Google Cloud Functions 會拋出“配置 firebase.databaseURL 的無效值”錯誤?

[英]Why is Google Cloud Functions throwing a "Invalid value for config firebase.databaseURL" error when I try to initialize a firebase app?

我有一個 Google Cloud Function,可以將存在信息從 Firebase 實時數據庫同步到 Firestore 數據庫(如此 所述)。 這是鏈接示例中的相關 Cloud Functions 代碼:

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

admin.initializeApp();

// Since this code will be running in the Cloud Functions enviornment
// we call initialize Firestore without any arguments because it
// detects authentication from the environment.
const firestore = admin.firestore();

// Create a new function which is triggered on changes to /status/{uid}
// Note: This is a Realtime Database trigger, *not* Cloud Firestore.
exports.onUserStatusChanged = functions.database.ref('/status/{uid}').onUpdate(
    (change, context) => {
      // Get the data written to Realtime Database
      const eventStatus = change.after.val();

      // Then use other event data to create a reference to the
      // corresponding Firestore document.
      const userStatusFirestoreRef = firestore.doc(`status/${context.params.uid}`);

      // It is likely that the Realtime Database change that triggered
      // this event has already been overwritten by a fast change in
      // online / offline status, so we'll re-read the current data
      // and compare the timestamps.
      return change.after.ref.once('value').then((statusSnapshot) => {
        const status = statusSnapshot.val();
        console.log(status, eventStatus);
        // If the current timestamp for this data is newer than
        // the data that triggered this event, we exit this function.
        if (status.last_changed > eventStatus.last_changed) {
          return null;
        }

        // Otherwise, we convert the last_changed field to a Date
        eventStatus.last_changed = new Date(eventStatus.last_changed);

        // ... and write it to Firestore.
        return userStatusFirestoreRef.set(eventStatus);
      });
    });

我最近收到一封來自 Google 的電子郵件,通知我需要從 NodeJS 6 更新到 NodeJS 8 或 10。由於此特定功能尚未投入生產,我繼續在 Google Cloud Console 中進行配置更改。 我現在收到以下錯誤。 我嘗試切換回 NodeJS 6,從頭開始重新創建函數,檢查 Github 問題和其他在線論壇。 似乎不再為我的 Google Cloud Function 提供連接 Firebase/Firestore 所需的環境變量。 但是,我不確定為什么會這樣。

Error: Invalid value for config firebase.databaseURL: undefined
  at resourceGetter (/srv/node_modules/firebase-functions/lib/providers/database.js:101:19)
  at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:102:13) 
  at /worker/worker.js:825:24 
  at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7)

此錯誤還顯示在 Cloud Functions 的 Stackdriver 日志中:

Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail

您應該使用 Firebase CLI 重新部署。 它在環境中做了一些特殊的事情來幫助 Firebase Admin SDK 在沒有任何參數的情況下正確初始化(添加 FIREBASE_CONFIG)。 聽起來當您在控制台中更改運行時時,您也丟失了這個特殊配置。

對我來說,我使用 firestore,我遇到了和你一樣的錯誤,所以我不得不創建一個沒有任何記錄的實時數據庫,然后我像這樣為管理員設置憑據:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp({
  databaseURL: "your realtime database url"
});

完成后,運行firebase deploy --only functions來部署您的函數。

這是您的實時數據庫 URL:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM