在我的Firestore中,我有一些文档,这些文档代表了我正在使用Firebase函数和打字稿开发的项目的论坛帖子。
firestore结构如下:
{
forumPosts: {
:forumPostId: {
id: string,
content: string,
icon: string,
...
}
}
}
现在,我想允许用户更新他的帖子。查询的主体应仅包含需要更新的字段:假设我要更新帖子的内容,查询主体应如下所示:
{
content: "updated content"
}
当前用于处理更新的firebase函数如下所示。
export const updateNewForumPost=functions.https.onRequest((req: PR_UserRequest, res: Response) => {
const fieldsToUpdate = {
id: req.body.id,
content: req.body.content,
icon: req.body.icon,
...
}
db.collection("forumPosts").doc(req.params.postId).update( fieldsToUpdate )
.then(() => {
res.json({ message: `forum post ${req.params.postId} updated successfully` })
})
.catch((error) => res.json({ error }))
});
因此,我在forumPost的每个字段中都放入了“要更新的字段”对象,并且不可避免地,查询中有很多字段具有不需要更新的旧值。
正如我所说,我试图找到一种方法来仅将用户在其请求中放入的字段放入该对象。 对我应该如何处理此问题有任何想法吗?
谢谢。