2017-04-04 30 views
0

函数正在观察proposals/{jobid}/{propid}。当添加新建议并且child("isinvitation")为空时,该功能成功地将新节点写入proposals/sent,然后将增量添加到作业jobs/${jobid}的建议子项。Firebase的云端函数在执行前中断

当提案被移除时,该功能失效。 userRef.child(jobid).remove()也不触发,减少对作业jobs/${jobid}的提案子不发生。

exports.CountProposals = functions.database.ref("/proposals/{jobid}/{propid}").onWrite((event) => { 
    const jobid = event.params.jobid; 
    const userId = event.params.propid; 
    const isinvitation = event.data.child("isinvitation").val(); 
    if (!isinvitation) { 
     const userRef = admin.database().ref(`users/${userId}/proposals/sent`); 
     if (event.data.exists() && !event.data.previous.exists()) { 
      userRef.child(jobid).set({ 
       timestamp: admin.database.ServerValue.TIMESTAMP 
      }); 
     } else if (!event.data.exists() && event.data.previous.exists()) { 
      userRef.child(jobid).remove(); 
     } 
    } 
    const collectionRef = admin.database().ref(`/jobs/${jobid}`); 
       return collectionRef.once('value').then(snapshot => { 
         if (snapshot.val() !== null) { 
          const countRef = collectionRef.child("proposals"); 
          countRef.transaction(current => { 
              if (event.data.exists() && !event.data.previous.exists()) { 
                  return (current || 0) + 1; 
              } else if (!event.data.exists() && event.data.previous.exists()) { 
                  return (current || 0) - 1; 
              } 
          }); 
         } 
       }); 
}); 

控制台日志不显示任何错误。

回答

0

你的函数试图在几个地方做几次写操作。这些写作中的每一个都将产生一个跟踪其完成的不同承诺。你应该返回一个单一的承诺,当所有的工作完成时解决。就目前而言,你只是从collectionRef.once('value').then()中返回一个单一的承诺,它本身并没有返回跟踪交易完成的另一个承诺。

基本上,您需要小心跟踪所有带有承诺的写入,并且通常使用Promise.all()来等待所有未完成的工作。

+0

我正在处理event.data.child(“isinvitation”).val()的空值。对我来说,使用之前的值是没有意义的。 –

+0

我不清楚你的功能应该做什么。你主要说的是它不是*做的。还要记住,你需要从一个可以工作的函数中返回一个承诺,比如更新数据库。现在你没有使用从更新(remove,transaction)返回的任何promise。 –

+0

当新数据和isinvitation = false写入或删除数据库'users/$ {userId}/proposals/sent'中的数据,然后增加或减少'/ jobs/$ {jobid}中的值提议时, –

0

“每次发生Firebase实时数据库写入时都会触发的事件处理程序”。 - https://firebase.google.com/docs/reference/functions/functions.database.RefBuilder#onWrite

我个人已经期待得到触发了删除操作为好,但是,它可能类似于AngularFire,其中write操作不被认为是remove操作。

你可以看看这个,看看你能得到它与你的情况下工作:https://firebase.google.com/docs/reference/functions/functions.database.DeltaSnapshot#changed

编辑:尝试一些我自己的职能,他们似乎触发取消对我之后。我会更多地研究你的代码。

相关问题