Reputation: 530
I've created this function:
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];
}
Now when I call this function using console.log(await getRoomTempFromRoomId("1234"))
, I receive the following error:
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.
The same code works on other Firebase projects
Upvotes: 0
Views: 895
Reputation: 317878
You're not showing it in your question, but the function that contains the call to await getRoomTempFromRoomId
is itself not marked async
. You can only use await
if the function that immediately encloses it is an async
function. Not that await will still not work if it's inside an anonymous function that's inside an async function, because the callback function itself is not async.
Upvotes: 2