【问题标题】:Cloud Functions for Firebase suddenly timing outFirebase 的 Cloud Functions 突然超时
【发布时间】:2017-03-29 16:18:18
【问题描述】:

今天部署我的函数后,我们的一些函数突然总是超时。我在云功能控制台中看到了这一点。这似乎在嵌套和返回承诺时发生。奇怪的是这种情况以前从未发生过。

这是我的代码。

exports.pushMessageQueue = functions.database.ref('/userPushMessagesQueue/{userId}/{messageId}').onWrite(event => {
    if (!event.data.exists()) {
        console.log('DOES NOT EXIST!');
        return "not exists";
    }

    console.log("EXISTS!");

    const userId = event.params['userId'];
    const messageId = event.params['messageId'];

    const payload = event.data.val();

    return database.ref('devices').orderByChild('user_id').equalTo(userId).once('value', snapshot => {
        if (!snapshot.exists()) {
            return "no devices found";
        }

        const devices = snapshot.val();

        const deviceTokens = [];

        snapshot.forEach(deviceSnap => {
            const device = deviceSnap.val();
            deviceTokens.push(device['fcm_key']);
        });

        const removeFromQueue = database.ref(`/userPushMessagesQueue/${userId}/${messageId}`);

        console.log('then0');

        return removeFromQueue.set(null).then(() => {
            console.log('then1');
            return admin.messaging().sendToDevice(deviceTokens, payload).then(() => {
                console.log('then2');
                return "send";
            });
        }, (error) => {
            console.log('error1!');
            console.log(error);
        });
    });
});

在控制台中记录 then0,而不是 then1 和 then2。当我收到推送通知并且条目从队列中删除时。

是不是我做错了什么?

【问题讨论】:

  • 从今天开始,我的 Firebase 云功能也遇到了类似的问题。有什么可用的修复方法吗?

标签: javascript firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

您返回的是 once() 函数,而不是承诺链。

基本上,你已经写了这个:

//returns the once promise
return ref.once('value', function(snap) { 
  /* some stuff that doesn't affect the promise chain happens here */
});

当你想要的是这样的:

// returns whatever happens in the .then() callback
return ref.once('value').then(snap => {
   /* now whatever you return here affects the promise chain */
});

【讨论】:

    猜你喜欢
    • 2018-01-03
    • 2018-04-19
    • 2017-08-26
    • 2019-03-20
    • 2017-09-01
    • 1970-01-01
    • 2021-09-27
    • 2018-06-11
    • 1970-01-01
    相关资源
    最近更新 更多