0

I am trying to set an array of messages once I have loaded all necessary data from firestore, but it seems I have one too many await/asyncs in there as the output results in

[{"_A": null, "_x": 0, "_y": 0, "_z": null},....]

function loadMessages() {

    const newMessageArray = chatIds.map(async chat => {

      const q = query(collection(db, collections.messages),
        where('chatId', '==', chat.id),
        orderBy("createdAt", 'desc'),
        limit(1)
      );

      return await getDocs(q).then(async querySnapshot => (querySnapshot.docs[0].data()));
    });
    console.log(newMessageArray)
    setMessages(newMessageArray);
  }

How can I fix it in a way that leaves me with a single new array for setMessages?

1 Answer 1

0

The problem is because you're returning a Promise in your return of the .map. You need to solve them.

This link will help you. You'll need to use the Promise.all to solve them.

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.