【问题标题】:Incremential operation with Cloud Functions for Firebase使用 Cloud Functions for Firebase 进行增量操作
【发布时间】: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


【解决方案1】:

问题是我没有对我的顶级功能做出承诺。这导致 Google Cloud Functions 在操作完成之前将其杀死。

添加一个承诺解决了我的问题

admin.database().ref(pathToAttendees).once("value").then( function(snapshot) {  

【讨论】:

    猜你喜欢
    • 2018-12-03
    • 2017-10-09
    • 2017-09-16
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2018-01-03
    相关资源
    最近更新 更多