Ad
Google Cloud / Firebase Functions: Await Is Only Valid In Async Function
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
Ad
Answer
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.
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad