我正在运行一个由 firebase 实时数据库更改和更新 FireStore 触发的云函数。但是,虽然函数触发,但函数无法访问 Firestore。
exports.reveal = functions.database.ref('/reveals/{postIDthatWasRevealed}/revealed').onUpdate((change, context) => {
const revealedValue = change.after.val()
if (revealedValue === true) {
var updates = {}
const postID = context.params.postIDthatWasRevealed
return admin.firestore().document('/posters/' + postID).get().then(querySnapshot => {
Run Code Online (Sandbox Code Playgroud)
此时,控制台日志指出TypeError:admin.firestore(...).document is not a function at..我已经尝试过这个答案:尝试过答案但问题仍然存在。这是否与我在 firebase 云功能内部访问 firestore 的事实有关?我的云功能是否没有正确更新?
编辑(包括初始化代码)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
Run Code Online (Sandbox Code Playgroud)
此外,我尝试通过将其更改为来调试该功能:
if (revealedValue === true) {
var updates = {}
const postID = context.params.postIDthatWasRevealed
return admin.firestore()
Run Code Online (Sandbox Code Playgroud)
现在,当我触发该函数时,我得到Function execution took 811 …
尝试按照本页上的说明进行操作,但我不知道如何简单地获取 DocumentSnapshot 中的数据。
他们并没有真正解释更新 onCreate,只是说它与 onDelete 相同,但我无法让它工作。
exports.mFoo = functions.firestore
.document('foos/{key}')
.onCreate((snap, context) => {
const bar = snap.data(); // <-- DOESN'T WORK
console.log(bar); // <-- DOESN'T WORK
return Promise;
});
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
类型错误:snap.data 不是exports.mFoo.functions.firestore.document.onCreate 中的函数
我确信这非常简单,但我不太理解这些东西,而且我尝试了很多东西的组合,但没有任何效果。
我需要帮助,但我总是在firestore云功能admin.firestore.collection上未收到错误消息。我在做什么错了我忘记了一些东西还是需要改变一些东西?
这是代码
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.firestore.document('users/{userID}/notifications/{notificationID}').onWrite((change, context) => {
const to_user_id = context.params.userID;
const notification_id = context.params.notificationID;
console.log('We have notification from: ' + to_user_id + ' The notification id is: ' + notification_id);
return admin.firestore().collection('users').doc(to_user_id).collection('notifications').doc(notification_id).get().then(queryResult => {
const from_user_id = queryResult.data().commentUID;
const from_user_data = admin.firestore.collection('users').doc(from_user_id).get();
const to_user_data = admin.firestore.collection('users').doc(user_id).get();
return Promise.all([from_user_data, to_user_data]).then(result => {
const from_name = result[0].data().name;
const to_name = result[1].data().name;
return console.log("FROM: " + from_name + " TO: …Run Code Online (Sandbox Code Playgroud) node.js firebase google-cloud-functions google-cloud-firestore