【问题标题】:like does not remove from post schema喜欢不会从帖子模式中删除
【发布时间】:2021-05-28 16:42:15
【问题描述】:

like 不会从 post 模型中删除

likeSchema
    .findOne({$and: [{post: postId ,user: userId}]})
    .exec((err, result) =>{
        if (result) {
            db.likeSchema
                .findOne(
                    { 
                        $and: [
                            { post: postId },
                            { user: userId }
                        ]
                    }
                )
                .exec((err, like) => {
                    db.likeSchema
                        .findOneAndUpdate(
                            { 
                                $and: [
                                    { post: postId },
                                    { user: userId }
                                ] 
                            }
                )
                .remove()
                .exec(() => {
                    db.postSchema
                        .findOneAndUpdate(
                            { _id: postId.postID },
                            { 
                                $pull: { 'likes': like._id }
                            }
                        )
                        .exec((err, post) => {
                       
                        })
               })
            })
        }
const likeSchema = new schema({
    post: { type: schema.Types.ObjectId, ref: "postSchema" },
    user: { type: schema.Types.ObjectId, ref: "userSchema" }
})
const postSchema = new schema({
    owner: { type: schema.Types.ObjectId, ref: "userSchema" },
    text: String,
    image: String,
    comments: [{ type: schema.Types.ObjectId, ref: "commentSchema" }],
    likes: [{ type: schema.Types.ObjectId, ref: "likeShcema" }],
    date: { type: Date, default: Date.now }
}); 

【问题讨论】:

  • 请提供LikeSchemaPostSchema
  • const likeSchema = new schema({ post: { type: schema.Types.ObjectId, ref: "postSchema" }, user: { type: schema.Types.ObjectId, ref: "userSchema" } , }) const postSchema = new schema({ owner: { type: schema.Types.ObjectId, ref: "userSchema" }, text: String, image: String, cmets: [{ type: schema.Types.ObjectId, ref: "commentSchema" }], 喜欢:[{ type: schema.Types.ObjectId, ref: "likeShcema" }], date: { type: Date, default: Date.now } });

标签: javascript node.js mongodb mongoose nosql


【解决方案1】:

试试这个代码

如果你想删除'post'中的'like',你应该删除它的文档。

LikeModel
        .findOneAndDelete({post: postId ,user: userId})
        .exec((err, like) => {
            PostModel
                .findByIdAndUpdate(like.post,
                    { 
                        $pull: { 'likes': like._id }
                    }
                )
                .exec()
                .then((err, post) => {
                    console.log(post)
                })
        })

更新

别忘了注册你的架构

const LikeModel = mongoose.model('Like', likeSchema)
const PostModel = mongoose.model('Post', postSchema)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 2020-11-23
    • 2020-11-04
    • 2020-09-11
    相关资源
    最近更新 更多