我创建了这个函数:
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
本身没有标记为async
。仅await
当立即包含它的async
函数是函数时才能使用。如果 await 位于异步函数内的匿名函数内,则不是说它仍然无法工作,因为回调函数本身不是异步的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句