gpt4 book ai didi

node.js - 当我尝试初始化 Firebase 应用程序时,为什么 Google Cloud Functions 会抛出 "Invalid value for config firebase.databaseURL"错误?

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:43 26 4
gpt4 key购买 nike

我有一个 Google Cloud Function,可以将状态信息从 Firebase 实时数据库同步到 Firestore 数据库(如 here 所述)。这是链接示例中的相关 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 Function 的 Stackdriver 日志中:

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

最佳答案

您应该使用 Firebase CLI 重新部署。它在环境中执行一些特殊操作,以帮助 Firebase Admin SDK 在没有任何参数的情况下正确初始化(添加 FIREBASE_CONFIG)。听起来当您在控制台中更改运行时时,您也丢失了这个特殊配置。

关于node.js - 当我尝试初始化 Firebase 应用程序时,为什么 Google Cloud Functions 会抛出 "Invalid value for config firebase.databaseURL"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55767623/

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