Skip to content
Advertisement

Undefined from promise using .then() JavaScript

I am trying to get data from Firestore using multiple queries, but the returned data is always undefined, even though console.log(data) in .then() block shows the data exist.

JavaScript

Advertisement

Answer

You have to return the promise in your function getPeers():

JavaScript

EDIT:

A little advice when handling errors in async functions, in your code you have the catch() handler with a console.log:

JavaScript

To avoid this you could throw an error in catch() like this:

JavaScript

So then you can check if an error ocurred like this:

JavaScript

You can see more about async functions here.

Remember this:

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement