Zoker
Zoker

Reputation: 2059

Get returned data from async function

With the following async function I get data from a firebase firestore instance:

export async function getSomething(db, id) {
  db.collection('someting').doc(id).get().then((doc) => {
    if (doc.exists) {
      return doc.data();
    }
    throw new Error('No such document!');
  }).catch((error) => {
    throw new Error('err', error);
  });
}

I called this function like:

getSomething(db, this.id).then((data) => {
  console.log(data); // data is empty here
}).catch((err) => {
  console.log(err);
});

The issue is, that on the data from the then function is empty. How can I get the data from the getSomething function? Is returning the data not enough?

Upvotes: 0

Views: 71

Answers (1)

Quentin
Quentin

Reputation: 943108

If you want to get a value back from a function, then you need to return something from it.

You have a number of return statements, but they are all inside callbacks.

You need one for getSomething itself.

return db.collection('someting').doc(id).get().then((doc) => {

Upvotes: 3

Related Questions