【发布时间】:2021-10-16 14:49:59
【问题描述】:
我正在使用 Admin SDK 编写一个 Firebase 函数,该函数应该从 Firestore 中检索数据(在 RealTime DB 插入事件时触发的函数)。
看起来 Relatime DB 运行良好,但是当我添加 Firestore 代码时,它在 Firestore 连接上失败,并在部署时出现以下错误:
Error: Missing expected firebase config value databaseURL, config is actually{"firebase":{"projectId":"simplify-poc","databaseURL":"https://simplify-poc-default-rtdb.firebaseio.com","storageBucket":"simplify-poc.appspot.com","locationId":"us-central"}}
If you are unit testing, please set process.env.FIREBASE_CONFIG
这是我的功能代码,我错过了什么?
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
async function getHelperToken(helperId) {
//Get token from Firestore
const tokensRef = db.collection("tokens");
const helper_token = await tokensRef
.where("user", "==", helperId)
.get("device_token");
return helper_token;
}
//DB triggered function - upon writing a child in the HelpersInvitations reference
exports.sendHelperInvitation = functions.database
.ref("/HelpersInvitations/{helper_invitation_id}")
.onCreate((snapshot, context) => {
const studentId = snapshot.val().studentId;
const studentName = snapshot.val().studentName;
const helperId = snapshot.val().helperId;
const title = snapshot.val().title;
const body = snapshot.val().body;
//Get the helper token by Id
const helper_token = getHelperToken(helperId);
//Notification payload
const payload = {
notification: {
title: title,
body: body,
icon: "default",
click_action: "com.skillblaster.app.helperinvitationnotification",
},
};
// //Send the notification
return admin.messaging().sendToDevice(helper_token, payload);
});
【问题讨论】:
标签: firebase google-cloud-firestore google-cloud-functions