0

I have an asynchronous function in a react-native project that does work on firebase's firestore. my issue is NOT with returning data from an async function. After getting some data, I have mapped it, and need to return from inside map

async updateConvo() {
    const snapshot = await firebase
      .firestore()
      .collection("conversations")
      .where(
        "members",
        "array-contains",
        this.state.recipient && this.props.user.email
      )
      .get();

    const arr = snapshot.docs.map(doc => {
      alert("inner alert: " + doc.id);
      return doc.id;
    });
    alert("outer alert: "+ arr[0]);
    return arr[0];
  }

Inner alert always executes and I get see the alert eg "inner alert: document-ID "

The outer alert never fires, and when I just return arr[0] its undefined when I implement the updateConvo function elsewhere.

How to I successfully return doc.id from the map function and finally the whole function?

Jim
  • 1,988
  • 6
  • 34
  • 68
  • Your `.map` callback isn't returning anything. Return a Promise instead and `await` it (with `Promise.all` if you have more than one item in the array) – CertainPerformance Oct 24 '19 at 22:59
  • how is the map callback not returning anything? `return doc.id` is in `.map()` scope – Jim Oct 24 '19 at 23:06
  • That occurs *before* the asynchronous action is complete, which is the problem - you need to wait for firebase to finish working. Return the Promise instead – CertainPerformance Oct 24 '19 at 23:07
  • im not actually returning data from the second firebase call. so I dont know why id be returning the promise. which is why this question shouldn't have been marked. the data `doc.id` is available regardless of the second firebase call – Jim Oct 24 '19 at 23:13

0 Answers0