0
votes

I am building a simple react-native app using firebase, in which users can create posts, see lists of other users posts, and posts can be saved as a "saved posts" as a sub-collection within my users collection.

I am new to react-native and firebase so this is just for educational purposes.

I am getting stuck on querying the posts for my 'saved' list. Currently, when a user clicks the 'save' button, a sub-collection of 'saved posts' is added to (or created) of the post id. I do not want to rewrite the entire post data to the users saved list, only to create a reference to the post via the post id.

I am able to save the list, but have not been able to query firestore for the documents associated with each post id.

Currently I can query based on the users id to retrieve the list of their own posts, as the authorID matches the user uid, and I thought getting the saved posts would be similar.

Here is my code for getting a users own list of posts:

userId is a single string representing user.uid

const getUserPosts = async (userId) => {
    const snapshot = await firebase
      .firestore()
        .collection('posts')
        .where('authorID', '==', userId)
        .get();
    let data = snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
    return data;
};

This function is exported and then wrapped in a try catch statement within the component.

The difference between the above and getting the users list of saved posts is that I have multiple different "postId's" which I need to input (vs a single userID), but the .get() method will not accept an array for the first argument, it requires a single string.

To quickly summarize I need a method or function in which I can take an array of postId's and query my posts collection to return those posts in which the doc.id matches the postId's provided.

I apologize if this is unclear or confusing, I am getting a bit lost on this one.

Any suggestions would be very welcomed.

Thank you so much for your time!

2

2 Answers

3
votes

You can do a query for multiple document IDs at once using an "in" query and using FieldPath.documentId():

const array = [...];
const snapshot = await firebase
  .firestore()
    .collection('posts')
    .where(firebase.firestore.FieldPath.documentId(), 'in', array)
    .get();

But this only works if the array is of less then 10 items long (Firestore limitation). If you need more, you'll have to either batch the IDs, into smaller arrays and perform multiple queries, or simply iterate the IDs, and get() each document individually (which is just fine, really, don't worry about performance on that).

1
votes

It's working :)

Big Thank you to Doug Stevenson for your help!!

Here is the function:

postedId is an array of id's referring to the id of saved posts from the 'posts' collection

`export const getSaveData = async (postedId) => {
 const array = [postedId];
 const snapshot = await db.collection('posts').get();
 const data = snapshot.docs.map((doc) => ({ postedId: doc.id, 
 ...doc.data() }));
 return data;
 };`