首页 > 解决方案 > 我的 firebase 云函数告诉我:函数执行耗时 60004 毫秒,状态为“超时”,但为什么呢?我的代码有什么问题:(

问题描述

我的英语不是最好的(很抱歉),我对 firebase 的东西很陌生。

有人可以告诉我(如果你还有一些时间给我),为什么我的云函数在日志中告诉我:函数执行耗时 60004 毫秒,完成状态为:“超时”。该功能运行良好,但我不喜欢服务器日志^^

也许我的代码也完全错误地编程了,但是,是的,正如我所说的“我是新手”,我会尽力变得更好:)

这是我完整的函数代码:

exports.winSumCountRound = functions.database.ref('/casebattle/{id}/currentCountRound').onUpdate(async (snapshot, context) => {
    let currentCountRound = snapshot.after.val();

    if (currentCountRound >= 1) {
        const pathId = context.params.id;
        const caseCount = (await admin.database().ref('/casebattle/'+pathId+'/caseCount').once('value')).val();
        const users = (await admin.database().ref('/casebattle/'+pathId+'/users').once('value')).val();
        const currentWinSum = (await admin.database().ref('/casebattle/'+pathId+'/winSum').once('value')).val();

        const getWinSumArray = await users.map(async (user, index) => {
            const wonItemUser = (await admin.database().ref('/casebattle/'+pathId+'/itemWins/'+index+'/'+(currentCountRound - 1)).once('value')).val();

            currentWinSum[index] = currentWinSum[index] + wonItemUser.price;

            if (typeof wonItemUser.sticker_1 !== 'undefined' && wonItemUser.sticker_1 !== false) {
                currentWinSum[index] = currentWinSum[index] + wonItemUser.sticker_1.price;
            }
            if (typeof wonItemUser.sticker_2 !== 'undefined' && wonItemUser.sticker_2 !== false) {
                currentWinSum[index] = currentWinSum[index] + wonItemUser.sticker_2.price;
            }
            if (typeof wonItemUser.sticker_3 !== 'undefined' && wonItemUser.sticker_3 !== false) {
                currentWinSum[index] = currentWinSum[index] + wonItemUser.sticker_3.price;
            }
            if (typeof wonItemUser.sticker_4 !== 'undefined' && wonItemUser.sticker_4 !== false) {
                currentWinSum[index] = currentWinSum[index] + wonItemUser.sticker_4.price;
            }
        });

        return Promise.all(getWinSumArray).then( async () => {
            await admin.database().ref('casebattle/' + pathId + '/winSum').set(currentWinSum);

            if(currentCountRound >= caseCount){
                return await new Promise(() => setTimeout(async () => {
                    await admin.database().ref('casebattle/' + pathId + '/winSumFinished').set(true);
                    await admin.database().ref('/casebattle/'+pathId+'/isRunning').set(false);

                    return await users.map(async (user) => {
                        await admin.database().ref('/playedbattles/'+user.userId+'/'+pathId+'/isRunning').set(false);
                        return await admin.database().ref('/playedbattles/'+user.userId+'/'+pathId+'/winSumFinished').set(true);
                    });
                }, 2000));
            } else {
                return await new Promise(async () => {
                    return await admin.database().ref('casebattle/' + pathId + '/currentCountRound').transaction(currentCountRound => currentCountRound + 1);
                });
            }
        });
    } else {
        return null;
    }
});

非常感谢你帮助我♥</p>

标签: javascriptfirebasegoogle-cloud-functions

解决方案


如上所述,您的问题在于创建Promise对象而不是传入或调用resolve(). 这意味着你的最终承诺永远不会解决。

您还有一系列应在Promise.all.

我会创建一个简单的、基于承诺的超时函数,它应该让事情变得更容易

const timeoutPromise = (delay, fn)
  => new Promise(resolve => setTimeout(() => resolve(fn()), delay))

然后在你的承诺链的最后一部分......

if (currentCountRound >= caseCount) {
  await timeoutPromise(2000, async () => {
    await admin.database().ref('casebattle/' + pathId + '/winSumFinished').set(true);
    await admin.database().ref('/casebattle/' + pathId + '/isRunning').set(false);

    await Promise.all(users.map(async (user) => {
      await admin.database().ref('/playedbattles/' + user.userId + '/' + pathId + '/isRunning').set(false);
      await admin.database().ref('/playedbattles/' + user.userId + '/' + pathId + '/winSumFinished').set(true);
    }));
  });
} else {
  await admin.database().ref('casebattle/' + pathId + '/currentCountRound').transaction(currentCountRound => currentCountRound + 1);
}

推荐阅读