【问题标题】:My firebase cloud function tells me: Function execution took 60004 ms, finished with status: 'timeout', but why? What is wrong with my code :(我的 firebase 云函数告诉我:函数执行耗时 60004 毫秒,状态为“超时”,但为什么呢?我的代码有什么问题:(
【发布时间】:2020-06-04 04:20:10
【问题描述】:

我的英语不是最好的(很抱歉),而且我对 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;
    }
});

非常感谢你帮助我♥

【问题讨论】:

  • 你有return await new Promise(() => ... 没有传入或调用resolve()。我非常怀疑您是否需要创建 Promise 对象
  • 另外,users.map()前面不需要await
  • @Phil 非常感谢您的快速回复和提示 :)
  • @Phil 但是,如果我删除了 Promise,我需要返回一些东西并且我无法返回 setTimeout:/
  • @Phil 如果我把这行“return await new Promise(() => setTimeout(async () => {” 变成这个 return setTimeout(async () => { 我收到错误消息:序列化返回值时出错:TypeError: Converting circular structure to JSON

标签: javascript firebase google-cloud-functions


【解决方案1】:

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

您还有一组应在Promise.all 内的承诺。

我会创建一个简单的、基于 Promise 的超时函数,它会让事情变得更简单

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);
}

【讨论】:

  • 非常感谢!它的工作♥函数执行耗时 2133 毫秒,完成状态:'ok'
猜你喜欢
  • 1970-01-01
  • 2020-12-01
  • 2020-09-13
  • 1970-01-01
  • 2022-06-16
  • 2023-03-30
  • 2020-10-09
  • 1970-01-01
  • 2020-04-29
相关资源
最近更新 更多