[英]Why is Google Cloud Functions throwing a "Invalid value for config firebase.databaseURL" error when I try to initialize a firebase app?
I have a Google Cloud Function that syncs presence information from a Firebase realtime database to a Firestore database (as explained here ).我有一个 Google Cloud Function,可以将存在信息从 Firebase 实时数据库同步到 Firestore 数据库(如此 处所述)。 This is the relevant Cloud Functions code from the linked example:
这是链接示例中的相关 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);
});
});
I recently received an email from Google informing me that I will need to update from NodeJS 6 to NodeJS 8 or 10. As this particular function isn't in production yet, I went ahead and made the configuration change in the Google Cloud Console.我最近收到一封来自 Google 的电子邮件,通知我需要从 NodeJS 6 更新到 NodeJS 8 或 10。由于此特定功能尚未投入生产,我继续在 Google Cloud Console 中进行配置更改。 I now get the error below.
我现在收到以下错误。 I tried switching back to NodeJS 6, recreating the function from scratch, checking Github issues and other online forums.
我尝试切换回 NodeJS 6,从头开始重新创建函数,检查 Github 问题和其他在线论坛。 It appears that my Google Cloud Function is no longer being provided with the necessary environment variables to connect with Firebase/Firestore.
似乎不再为我的 Google Cloud Function 提供连接 Firebase/Firestore 所需的环境变量。 However, I'm unsure why that would be the case.
但是,我不确定为什么会这样。
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)
This error also shows up in the Stackdriver logs for the Cloud Function:此错误还显示在 Cloud Functions 的 Stackdriver 日志中:
Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail
You should redeploy using the Firebase CLI.您应该使用 Firebase CLI 重新部署。 It does some special things in the environment to help the Firebase Admin SDK initialize correctly without any parameters (adding FIREBASE_CONFIG).
它在环境中做了一些特殊的事情来帮助 Firebase Admin SDK 在没有任何参数的情况下正确初始化(添加 FIREBASE_CONFIG)。 It sounds like when you changed the runtime in the console, you also lost this special configuration.
听起来当您在控制台中更改运行时时,您也丢失了这个特殊配置。
For me, I use firestore, and I was getting the same error as you, so I had to create a real-time database without any record then I set the credentials for the admin like so:对我来说,我使用 firestore,我遇到了和你一样的错误,所以我不得不创建一个没有任何记录的实时数据库,然后我像这样为管理员设置凭据:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp({
databaseURL: "your realtime database url"
});
When you are done, run firebase deploy --only functions
to deploy your functions.完成后,运行
firebase deploy --only functions
来部署您的函数。
Here is your Realtime database URL:这是您的实时数据库 URL:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.