【发布时间】: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