【发布时间】:2019-08-30 21:10:43
【问题描述】:
我有 3 个收藏,用户、帖子和评论。
用户架构
{
posts: [{ type: Schema.Types.ObjectId, ref: 'Post' }],
likes: {
comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }],
posts: [{ type: Schema.Types.ObjectId, ref: 'Post' }],
}
}
发布架构
{
author: { type: Schema.Types.ObjectId, ref: 'User' },
likes: [{ type: Schema.Types.ObjectId, ref: 'User' }],
comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }],
}
评论架构
{
author: { type: Schema.Types.ObjectId, ref: 'User' },
post: { type: Schema.Types.ObjectId, ref: 'Post', },
}
现在这是我删除帖子以及与之相关的所有内容的步骤,例如从作者文档帖子字段中删除帖子 ID,用户喜欢帖子,cmets,用户喜欢 cmets。
这是我使用的功能,它可以工作,一切都很好,但我想知道是否有更好的方法来处理这个问题?
PostSchema.statics.deletePost = async function (postId) {
const post = await this.findById(postId);
const commentIds = post.comments;
await Promise.all([
User.findByIdAndUpdate(
post.author,
{ $pull: { posts: postId } },
),
User.updateMany(
{ _id: { $in: post.likes } },
{ $pull: { 'likes.posts': postId } },
),
commentIds.map(async (commentId) => {
const comment = await Comment.findById(commentId);
User.findByIdAndUpdate(
comment.author._id,
{ $pull: { comments: commentId } },
);
User.updateMany(
{ _id: { $in: comment.likes } },
{ $pull: { 'likes.comments': commentId } },
);
}),
this.findByIdAndDelete(postId),
]);
return post._id;
};
【问题讨论】:
-
对于干净的代码,您必须在每个承诺中使用
await。有了一系列的承诺,你应该使用Promise.all。否则你的代码没问题。