1

Im new to firebase, and im implementing a comment feature and i have this structure on the firestore:

 - User

     - UserID
     - Name
     - Picture


----------

 - Post
    - PostID
    - Comments
       - CommentID
       - PostID
       - UserID

I want to display the comments in each posts with the name and the picture of a user who commented. How can I do that? I have the userID of the commentor, is it possible to access the user User collection using that? Or is there something wrong with my database structure?

Im planning to include the name and the picture of the user inside the Comment collection but when the user updates their name and picture, how can i update the one on the Comment collection?

Ana
  • 25
  • 1
  • 3
  • It's not clear from your structure what is a `Collection`, what is a `Document` and what is a `Document field`. Could you try to be more explicit to make it easier to help you. – lenz Mar 20 '21 at 09:35
  • And are comments an array? A sub-collection? – lenz Mar 20 '21 at 09:36
  • See https://stackoverflow.com/questions/59061225/how-do-i-join-data-from-two-firestore-collections-in-flutter for some answers on how to implement the necessary client-side join – Frank van Puffelen Mar 20 '21 at 15:29

2 Answers2

1

This will get you the user with userID from the other collection.

FirebaseFirestore.instance
    .collection("User")
    .where("userID", isEqualTo: userID)
    .get();
lenz
  • 2,193
  • 17
  • 31
0

i don't know if i get what you've asked well, but you can try this.

FirebaseFirestore.instance
    .collection("Post").doc(PostID).collection("Comments")
    .where("userID", "==", userID)
    .get().then(snapshot =>{
           console.log(snapshot.docs.map(doc=>({id: doc.id, 
               data: doc.data()})))
      } )

The best way of dealing with this, is whenever the user posts a comment grab their data and post it in the comments collection, so that next time when you query you just get the comment and the person who commented.

Good Luck

crispengari
  • 7,901
  • 7
  • 45
  • 53