Problem with firebase function it is returned undefined value in a async function

177 views
Skip to first unread message

Javier Melo

unread,
Apr 29, 2020, 10:07:31 PM4/29/20
to Firebase Google Group
Hello: I am creating a function in firebase that calls other functions. This call is made asynchronously one by one. When I call the "foreachhours()" function it uses a loop to call another function called "getfilterhours()". When I bring the data from "getfilterhours()", when it is printed on screen it works correctly. But when I assign it to a variable it tells me that I need a promise, that's why there is the .then after the call. The problem is that when I return the Ddr data the result is either undefined or an empty [] array. What options do I have to return that value without it showing up with nothing?.

carbon.png

Thanks, for your help.

Sam Stern

unread,
Apr 30, 2020, 8:33:12 AM4/30/20
to Firebase Google Group
Hi Javier,

It looks like you're mixing a few programming styles, you have async/await but also promise chaining with then/catch.  It's very likely that your flow is incorrect and this is not an issue with Firebase, although it's hard to debug code from a screenshot (please send code as text in the future).

One thing that stands out is your first function which has something like this:

await array.forEach(async function (hora) {
   await somethingElse(hora) 
});

This will not work as you expect.  You cannot await a forEach loop with an async callback.  Instead try this style:

for (const hora of horasselected) {
  await somethingElse(hora);
}

- Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/0b9b4a1b-6c78-4113-8422-78f1c30abc5a%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages