This code is meant to create a mutation that allows a user to create a comment under the collection 'comments'.
createComment: builder.mutation({
queryFn: async(comment: Comment, postId: string) => {
try {
const batch = writeBatch(firestore);
const commentRef = doc(collection(firestore, 'comments'));
await updateDoc(commentRef, { id: commentRef.id });
batch.set(commentRef, comment);
const postRef = doc(firestore, 'posts', postId);
batch.update(postRef, { numberOfComments: increment(1) });
await batch.commit();
return { data: { commentRef } };
} catch (error) {
return { data: null };
}
},
invalidatesTags: ['Comment']
}),
It takes in two parameters. It then creates a write batch and sets the commentRef to the comments collection. It then updates the document with an id. The postRef is set to the firestore posts collection with the postId as an argument. The batch is then updated with an increment of 1 for numberOfComments. But nothing happens. when the function is called on the front.
const handleCreateComment = (comment: string, user: User, post: Post) => {
// create comment
const newComment: Comment = {
postTitle: post.title!,
createdBy: user.uid,
creatorDisplayName: user.email!.split('@')[0],
communityName: post.communityName!,
content: comment,
postId: post.id!,
createdAt: serverTimestamp() as Timestamp,
}
createComment(newComment, post.id);
setComment('');
setComments([...comments, newComment]);
}`
the only error I am facing the type script warning
Expected 1 arguments, but got 2.ts(2554)