Firebase云功能同步nodejs

问题描述 投票:1回答:1

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

enter image description here我很确定我正确地使用了承诺概念和“.then”..我的功能有什么问题? (与Alexandr Biship一起编辑)

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个回答
3
投票

如你所说,触发功能需要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()
            });

      });
© www.soinside.com 2019 - 2024. All rights reserved.