【问题标题】:Firebase cloud functions sync nodejsFirebase 云功能同步 nodejs
【发布时间】:2018-03-04 18:47:30
【问题描述】:

为什么我总是进入我的 firebase 控制台“Promise { pending }”?

我很确定我正确地使用了带有“.then”的承诺概念。我的函数有什么问题? (由亚历山大主教编辑)

exports.GenerateLiveGameIfAllPlayerReady = functions.database.ref("games/lobby/{pushId}/playerList/{playerId}").onUpdate(event => {
//Get the object of the game then put it in the Live games node
const transfertNewLiveGame = event.data.ref.parent.parent.once('value').then(snap => {
    return snap.val();
}).then(() => {
    console.log(transfertNewLiveGame);
    console.log(event.params.pushId);

    return admin.database().ref('games/live').update({
        [event.params.pushId]: transfertNewLiveGame
    });
})

【问题讨论】:

    标签: node.js firebase-realtime-database google-cloud-functions


    【解决方案1】:

    正如您所说,触发功能需要Promise,但您缺少return。所以你的代码应该是这样的。

    exports.GenerateLiveGameIfAllPlayerReady = functions.database.ref("games/lobby/{pushId}/playerList/{playerId}").onUpdate(event => {
    
          return event.data.ref.parent.parent.once('value', function(snap) {
    
                return admin.database().ref('games/live').update({
                    [event.params.pushId]: snap.val()
                });
    
          });
    

    【讨论】:

    • 好点!但是我仍然在控制台中收到错误。我刚刚用你的修改编辑了我的帖子。
    • 刚刚编辑了我的帖子。请检查并告诉我它是如何工作的。
    • 就像一个魅力!我仍然不明白为什么它不能与“const TransfertNewLiveGame”一起使用。实际上,我几乎从 Jenn 的视频教程中复制粘贴了:firebase.google.com/docs/functions/terminate-functionsNvm,谢谢!在我问之前至少 8 小时我就在这个问题上......
    • 很高兴听到这个消息。正如您所说,它需要 Promise 对象,但实际上您之前返回了已解决的结果。 .then()
    猜你喜欢
    • 1970-01-01
    • 2020-04-02
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2017-11-08
    • 2018-11-29
    相关资源
    最近更新 更多