【发布时间】:2017-05-10 12:38:17
【问题描述】:
我正在尝试使用 Cloud Functions for Firebase 为我的数据库创建一个函数。该函数的目的是监听attend表上的写入事件,并根据写入的对象来识别事件并递增事件对象上的usersAttending。
到目前为止,这是我的功能。
//listens to write on attendObjects (when a user is attending an event), and increments attending users for event
exports.listenAttendingEvents = functions.database.ref('/attend/{pushId}').onWrite(event => {
//get attendObj -> parsed JSON by javascript interpreter
const attentObj = event.data.val();
const attendId = attentObj['attendId'];
const pathToAttendees = '/attends' + '/' + attendId;
// Attach an asynchronous callback to read the data at our posts reference
admin.database().ref(pathToAttendees).on("value", function(snapshot) {
console.log(snapshot.val());
const obj = snapshot.val();
var nrAttending = obj['attending'];
nrAttending = Number(snapshot.val());
return admin.database().ref(pathToAttendees + '/attending').transaction(function (nrAttending) {
return (nrAttending || 0) + 1;
});
});
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
return errorObject
});
问题似乎是event object 没有被检索到。该功能似乎在此之前完成,状态为ok
【问题讨论】:
-
您没有从您的顶级函数返回承诺,这意味着 Google Cloud Functions 可能会在异步数据库事务仍在进行时终止它。阅读文档中的此页面了解更多信息:firebase.google.com/docs/functions/terminate-functions
-
@FrankvanPuffelen ...非常感谢...总是有帮助:)
标签: javascript node.js firebase google-cloud-functions