Home > front end >  How to update a sub-collection document when sub sub-collection is created in firestore using cloud
How to update a sub-collection document when sub sub-collection is created in firestore using cloud

Time:09-04

i have this functions that aimed to update the commentsCount field of a comment when sub comment is created but its not working which i dont know why. im hitting the correct document id to be updated but i dont know why its not updating. it doesn't have any error though. the console is running normally and says : `i functions: Beginning execution of "onCreateSubComment"

1d0HczjEvZM0T4u9iXb0 1d0HczjEvZM0T4u9iXb0 i functions: Finished "onCreateSubComment" in ~1s`

//this is working perfectly fine

export const onCreateComment = functions.firestore
    .document("posts/{postId}/comments/{commentId}")
    .onCreate(async (snap, context)=> {
      const postRefId = context.params.postId;
      const snapRef = snap.ref;
      const commentCollectionRef = snapRef.parent;
      const postRef = commentCollectionRef.parent;
      const postId = postRef?.id;
      try {
        return await db.collection("posts").doc(postId ?? postRefId)
            .update({commentsCount: admin.firestore.FieldValue.increment(1)});
      } catch (e) {
        functions.logger.log(e);
        throw e;
      }
    });

enter image description here

//not working

export const onCreateSubComment = functions.firestore
    .document("posts/{postId}/comments/{commentId}/subComments/{subCommentId}")
    .onCreate(async (snap, context) => {
      const commentRefId = context.params.commentId;
      const postId = context.params.postId;
      const snapRef = snap.ref;
      const subCommentCollectionRef = snapRef.parent;
      const commentRef = subCommentCollectionRef.parent;
      const commentId =commentRef?.id;
      console.log(commentId);
      console.log(commentRefId);
      try {
        return await db.collection("posts").doc(postId).collection("comments")
            .doc(commentId ?? commentRefId )
            .update({commentsCount: admin.firestore.FieldValue.increment(1)});
      } catch (e) {
        functions.logger.log(e);
        throw e;
      }
    });

enter image description hereg

enter image description here

CodePudding user response:

You need to put the field name between quotation marks, so instead of commentsCount use "commentsCount", like:

db.collection("posts").doc(postId).collection("comments")
  .doc(commentId ?? commentRefId )
  .update({"commentsCount": admin.firestore.FieldValue.increment(1)});

Besides, I would rather use set with option merge: true instead of update. This way the other fields won'be affected. Like this:

db.collection("posts").doc(postId).collection("comments")
  .doc(commentId ?? commentRefId )
  .set({"commentsCount": admin.firestore.FieldValue.increment(1)
  }, {merge: true});

CodePudding user response:

i solved it. nothing is wrong with the code. it is just conflicting my other functions. thanks to those who have given their time to help. y'all are awesome.

  • Related