Use async/await with TypeScript in Cloud Functions for Firebase - Firecasts
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about unresolved promises, undefined values and asynchronous Firebase calls in Cloud Functions, as a quick guide to async/await.
Recommended by 3 people across 3 different months, 2018 to 2022.
What it is
A short Firecasts episode from the Firebase channel in which Doug Stevenson converts Cloud Functions for Firebase code from promise chains to async/await, using TypeScript and ECMAScript 2017. It is part 2 of a series and comes with code on GitHub.
What people said
Recommends Doug Stevenson · 2018-08 · Stack Overflow · on “Variable stays undefined after assigning within Promise”
get() (as well as all functions that return a promise) are asynchronous , meaning they return immediately. The callback provided to then() gets executed some time later when the async work is complete. Your code looks like it's assuming that get() blocks execution of this code, which it does not. The subsequent code that accesses dish_data is going to execute immediately after get() returns, which means it will be undefined. It won't get defined until your callback is finally executed. If you want to use the results of the get() , you should only do that in the callback, or pass it along to some other function from there. If you want to use async , then you will have to also use await to eff
Recommends Củ Su Hào · 2018-10 · Stack Overflow · on “Unable to receive data from firebase database”
It returns an empty response because db.collection('artikel').get() returns a promise . It means it runs asynchronously . If you logged on Chrome, you can see the result after expanding the array (look at the [i] icon) You can simply fix the problem by adding async to your function and await after return: async function getArtikel (agent) return await db.collection('artikel').get() Check out this video [this video] for more information on async/await
Recommends Renaud Tarnec · 2019-11 · Stack Overflow · on “How to send firebase notifications using nodejs?”
There are two problems in your code: You don't return the promises returned by the asynchronous Firebase methods ( get() and sendToDevice() ); You mix-up the use of async/await with the then() [LINK] method. I would suggest you watch the 3 official videos about "JavaScript Promises" from the Firebase video series [LINK] , and then that you first try with using the then() method to correctly chain [LINK] your Promises and return the chain. The following code should work. exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot, context) => { const notificationMessageData = snapshot.data(); return admin.firestore().collection('pushTokens').get() .then(snapshot => {
Cites Jibin Thomas · 2019-02 · Stack Overflow · on “Getting Promise pending on Firebase cloud functions”
Using async/await solved the problem Video on using async await in cloud functions [this video] solution: console.log('getDataFromNode', await getDataFromNode()); async function getDataFromNode() { const node = await db.ref(`/locations/${parkingId}/price const data = await node.once('value'); return data.val(); }
Cites Chepe · 2019-03 · Stack Overflow · on “Type casting return from Firebase DB query to an iterable type”
What I am trying to do I'm currently setting up my Firebase Functions for the DB, for this I am using TypeScript. I'm trying to get a node from my database. The node is a Map (i.e. Map of String, bool in Java). The problem is that after fetching my node 'test' from my DB I am trying to iterate through its members, but I am getting the error The error TypeError: agenda.forEach is not a function at Object. (\firecast\functions\lib\index.js:32:16) at Generator.next () at fulfilled (\firecast\functions\lib\index.js:4:58) at process._tickCallback (internal/process/next_tick.js:68:7) My settup Here is how I have my DB set up: (Sorry, idk why the image embedding is not working, I'll put the link) L
Cites Renaud Tarnec · 2019-05 · Stack Overflow · on “Firebase Cloud Function error: Some values show as undefined?”
Instead of using the on() [LINK] method, which sets a listener "for data changes at a particular location", you need to use the once() [LINK] method, which "listens for exactly one event of the specified event type" and returns a Promise . You will then be able to use await to make the Cloud Function waiting until the promise returned by once() resolves and returns its result. You should therefore modify your code as follows: exports.gameDoneNotice = functions.database .ref('USERS/{termDate}/GameData/{myUID}/ExitDateTime') .onCreate(async (snapshot, context) => { const myNumber = context.params.myUID; const myRotation = context.params.termDate; var adminDB = admin.database(); exitTime = snap
Cites Royalelk · 2022-06 · Stack Overflow · on “Inconsistently getting 'FirebaseError: Response is not valid JSON object.'”
@DougStevenson I watched your video on async/await [this video] and I'm pretty confident all my code is correct (although you wrote it for onRequest and not onCall - I don't know if those have different behavior, although I would be shocked if they did).