Google Cloud / Firebase 函数:await 仅在异步函数中有效

伦纳德·尼豪斯

我创建了这个函数:

async function getRoomTempFromRoomId(roomId) {
    const querySnapshot = await db.collection("sensors").where("room", "==", roomId).where("type", "==", "temperature").limit(1).get();
    var values = []
    values = querySnapshot.docs.map((doc) => { return doc.data().value });
    return values[0];
}

现在,当我使用 调用此函数时console.log(await getRoomTempFromRoomId("1234")),收到以下错误:

SyntaxError: await is only valid in async function
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
⚠  Your function was killed because it raised an unhandled error.

相同的代码适用于其他 Firebase 项目

道格史蒂文森

您没有在问题中显示它,但是包含调用的函数await getRoomTempFromRoomId本身没有标记为asyncawait当立即包含它的async函数函数时才能使用如果 await 位于异步函数内的匿名函数内,则不是说它仍然无法工作,因为回调函数本身不是异步的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Javascript 函数 await 仅在异步函数中有效

for循环中的“ await仅在异步函数中有效”

For 循环 - SyntaxError: await 仅在异步函数中有效

给出 await 的异步函数仅在异步函数中有效

“await 仅在异步函数中有效”,而它是一个异步函数

await 总是抛出 SyntaxError: await 仅在 sequelize 的异步函数中有效

ASYNC / AWAIT SyntaxError:await 仅在异步函数和模块的顶级主体中有效

Javascript:SyntaxError:等待仅在异步函数中有效

SyntaxError:await 仅在带有 Apify Metamorph 的异步函数中有效

await 仅在异步函数 discord.js 中有效

Await仅在使用Node.js的异步函数中有效

“await 仅在异步函数中有效” discord.js

Sails.js SyntaxError:await 仅在异步函数中有效

await 仅在异步函数 Discord bot 反应角色中有效

await 仅在异步函数和模块的顶层主体中有效 javascript express error

控制台错误:await 仅在异步函数和模块的顶层主体中有效

即使等待位于 Node.js 中的异步函数中,如何修复“await 仅在异步函数中有效”?

等待仅在异步函数中有效,同时我已经在函数中放置了“异步”

Google Cloud / Firebase函数,处理每个函数的依赖性

`SyntaxError:await 仅在异步函数中有效`,同时尝试在 node rest api 中注册用户

SyntaxError: await 仅在异步函数和模块的顶层主体中有效。不和谐,js

module.exports SyntaxError:等待仅在异步函数中有效

module.exports SyntaxError:等待仅在异步函数中有效

Firebase / Google Cloud Function cron 函数返回 INVALID_ARGUMENT

未捕获的 SyntaxError:await 仅在 async 函数中的 async 函数中有效

Firebase Cloud函数中的Python与Node.js事件有效负载

从Firebase Cloud函数调用函数

如何从Firebase Cloud Function调用异步函数

SyntaxError:“await”仅在“async”函数中有效 - 需要帮助