0

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?

3
  • 2
    1) There's no reason for your function to be async since you're not using await. 2) return db.collection... Commented Dec 16, 2018 at 22:42
  • 2
    Why are you using an async function will there is no use of await operator? Commented Dec 16, 2018 at 22:43
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Dec 16, 2018 at 22:57

1 Answer 1

3

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) => {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.