此代码旨在创建一个突变,允许用户在“评论”集合下创建评论。
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']
}),
它有两个参数。然后它创建一个写入批处理并将 commentRef 设置为评论集合。然后它用一个 id 更新文档。 postRef 设置为以 postId 作为参数的 firestore posts 集合。然后,批次以 1 的增量更新 numberOfComments。但是没有任何反应。当在前面调用函数时。
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]);
}`
我面临类型脚本警告的唯一错误
预期 1 个参数,但得到 2.ts(2554)