Home > Net >  How to get data from multiple Id's in firestore in react native
How to get data from multiple Id's in firestore in react native

Time:03-14

I will get the data from multiple id's in firestore.

here is code:

const array = ['0phkMIPUevw9Ou7MXwIK', '0vWgD1ZdJmdR8zbQ2vba'];
    const rs1 = await firestore().collection('feed').doc(array).get()
    console.log("rs1 ---> ", rs1.data()) 

CodePudding user response:

Firestore doesn't expose API to fetch multiple documents in a single batch.

You can achieve that by fetching each document individually and merging results together into a single array.

async function fetchFeedPosts(postIds = []) {
  const promises = postIds.map(async (postId) => {
    const docSnapshot = await firestore().collection("feed").doc(postId).get();
    const docData = docSnapshot.data();

    return docData;
  });

  // Resolve all posts promise result into single array
  const feedPosts = await Promise.all(promises);

  return feedPosts;
}

// Call function later in your code

const postIds = ["0phkMIPUevw9Ou7MXwIK", "0vWgD1ZdJmdR8zbQ2vba"];

const feedPosts = await fetchFeedPosts(postIds);



  • Related