【问题标题】:Firestore Cloud Functions priorityFirestore 云功能优先级
【发布时间】:2018-10-24 16:54:30
【问题描述】:

Firestore 云函数优先级

现在我在 Firestore 数据库中部署了两个云函数。

它们是由相同的文档更改触发的。

是否可以指定函数的执行顺序或触发顺序?比如我想让updateCommentNum函数触发拳头,然后触发writeUserLog函数。我怎样才能实现这个目标?

exports.updateCommentNum = functions.firestore
.document('post/{postId}/comments/{commentsID}')
.onWrite((change, context) => 
{
    //update the comment numbers in the post/{postId}/
}


exports.writeUserLog = functions.firestore
.document('post/{postId}/comments/{commentsID}')
.onWrite((change, context) => 
{
    //write the comment name,text,ID,timestamp etc. in the collection "commentlog"

}

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    没有办法表示函数之间的相对优先级。

    如果您希望调用它们的定义顺序,请使用单个云函数并从那里调用两个常规函数:

    exports.onCommentWritten = functions.firestore
    .document('post/{postId}/comments/{commentsID}')
    .onWrite((change, context) => { 
      return Promise.all([
        updateCommentNum,
        writeUserLog
      ])
    })
    
    function updateCommentNum(change, context) {
        //update the comment numbers in the post/{postId}/
    }
    
    function writeUserLog(change, context) {
        //write the comment name,text,ID,timestamp etc. in the collection "commentlog"
    }
    

    这也将减少调用次数,从而降低运行它们的成本。

    【讨论】:

      猜你喜欢
      • 2018-05-09
      • 2011-06-13
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      相关资源
      最近更新 更多