首页 > 解决方案 > Firebase Cloud Functions 等待变量

问题描述

我想将 img_thumb_url 写入 firebase 数据库。在 func1A 中,我在下面编写的代码我希望当那些全局变量 room 和 msg_key 不为空时它可以突破 while 循环,但我发现它始终未定义,尽管当 func2B 成功触发时这些变量用字符串值定义。

...
            while(1){
                if(room!=null && msg_key!=null){
                    console.log('room is ', room, '. msg_key is ', msg_key);
                    console.log('break from the loop');
                    break;
                }
            }
            admin.initializeApp();
             return admin.database().ref('/msgs/' + room + '/chat/' + msg_key + '/attachment/photo/thumbnail/url2').set(img_thumb_url);

             })

但是 ' '/msgs/' + room + '/chat/' + msg_key + '/attachment/photo/thumbnail/url2 ' 上面的路径取决于从其他函数 func2B 检索到的变量 room 和 msg_key ,

exports.func2B = functions.database.ref('/msgs/{roomName}/chat/{pushid}')
    .onCreate((snapshot, context) => {
        // Grab the current value of what was written to the Realtime Database.
        const msg = snapshot.val();
        if (msg.attachPhotoUrl == null) {
            console.log('No photo attachment found');
            return null;
        }
        if (msg_key != null) {
            console.log('msg_key no longer null');
            return null;
        }

       console.log('writing url2 for thumbnail in firebase database...');

        msg_key = msg.key;
        room = msg.roomName;
        console.log('room is ',room);
        console.log('msg_key is ',msg_key);
        console.log('img_thumb_url: ',img_thumb_url);
      return ....
    });

我不确定这是否是正确的方法,但我不认为它这么简单。请帮助我如何解决。如何将 func2B 中的赋值变量 room 和 msg_key 获取到 func1A?

标签: firebasegoogle-cloud-functions

解决方案


推荐阅读