So I'm trying to get comments to go to the post they are put on. However, when I make a comment, a new collection gets made rather than it going to the post's ID.

How it looks: (the grey code at the bottom of this collection is the comment ID that was made after I added a comment. This is the problem.) enter image description here

How I want it to look: enter image description here

The firestore code I am using:

class FirestoreMethods {
  Future<void> postComment(String postId, String text, String uid, String name,
      String profilePic) async {
    try {
      if (text.isNotEmpty) {
        String commentId = const Uuid().v1();
        await postsRef
            .doc(uid)
            .collection('userPosts')
            .doc(postId)
            .collection('comments')
            .doc(commentId)
            .set({
          'profilePic': profilePic,
          'name': name,
          'uid': uid,
          'text': text,
          'commentId': commentId,
          'datePublished': DateTime.now()
        });
      } else {}
    } catch (e) {
      print(
        e.toString(),
      );
    }
  }
}

The collection that is made in Image 1 needs to go in to a subcollection of the userPosts as shown in image 2. How can I do this?


Solution 1: ItsHarsh.json

Could be an issue in your postsref so you can try something like this:

await FirebaseFirestore.instance.collection('posts').doc(uid).collection('userPosts').doc(postId).collection('comments').doc(commentId).set({
          'profilePic': profilePic,
          'name': name,
          'uid': uid,
          'text': text,
          'commentId': commentId,
          'datePublished': DateTime.now()
        });

Just make sure that you are passing proper uid, postId, and commentId