猫鼬模型数据未在 mongodb 内更新

Mongoose model data not updating in mongodb

我是 node.js 和 mongodb 的新手。我正在使用 express 休息-api。

我正在尝试创建一个 API,用户可以在其中 post 并点赞。我能够创建 post API 并且问题出在 API 用户可以点击赞按钮的地方,它会被计数,当他再次点击赞按钮时它会在我的例子中,在后端服务器(express 和 mongodb)中采取不同的方式并更新相同的方式。

架构定义

module.exports = mongoose.model('posts', new Schema({ 
    post: { type: String, required: true },
    date: { type: Date, required: true, default: moment.now() },
    author: { type: String, required: true },
    authorId: {type: String, required: true},
    likes: {type: Object, required: true, default: []},
    likesCount: { type: Number, required: true, default: 0},
    dislikes: { type: Object, required: true, default: [] },
    dislikesCount: { type: Number, required: true, default: 0 },
    comments: { type: Object, required: true, default: [] },
    commentsCount: { type: Number, required: true, default: 0 }
}, { timestamps : { createdAt: 'created_at' , updatedAt: 'updated_at' } }));

代码:

    router.post('/post/likes', function(req, res) {
    if (!_.isEmpty(req.body.postId) && !_.isEmpty(req.body.user) && !_.isEmpty(req.body.userId)) {

        //Finding the post by Id
        Posts.findById(req.body.postId, function(err, posts) {

            if (err) {
                return res.send(500).json({
                    success: false,
                    msg: err
                });
            }

            if (posts !== null) {
                var user = {
                    userId: req.body.userId,
                    user: req.body.user
                };
                //Returns the matched obj if true
                //Returns undefined if false
                var alreadyLiked = _.find(posts.likes, function(obj) {
                    if (obj.userId === user.userId && obj.user === user.user) {
                        return true;
                    } else {
                        return false;
                    }
                });


                if (alreadyLiked === undefined) {
                    posts.likes.push(user);
                    posts.likesCount = posts.likes.length;

                    var updatedPost = new Posts(posts);
                    updatedPost.save(function(err, data) {
                        if (err) {
                            return err;
                        }
                        res.status(200).send({
                            success: true,
                            data: data,
                            message: 'You have liked the post!'
                        });

                    });

                } else {
                    //Removing the already liked user object from posts.likes
                    posts.likes = _.without(posts.likes, _.findWhere(posts.likes, user));
                    posts.likesCount = posts.likes.length;

                    posts.markModified('likesCount');
                    var reupdated = new Posts(posts);
                    reupdated.save(function(err, data) {
                        if (err) {
                            return err;
                        }
                        res.status(200).send({
                            success: true,
                            data: data,
                            message: 'You have unliked the post!'
                        });

                    });

                }
            } else {
                res.status(200).send({
                    success: false,
                    message: 'No post found!'
                });
            }


        });
    } else {
        res.status(400).send({
            success: false,
            message: 'Bad request value'
        });
    }
});

问题是当我第一次喜欢 post 时它完美地工作并且 returns 预期的响应。

img: liked-response-img

当我通过调用相同的 API 与 post 不同时,它 returns 一些意外的结果。取消赞的用户已从点赞中移除 属性 但计数仍为“1”,如下图所示。我不明白为什么?有人可以指出我哪里做错了什么吗?提前致谢!

img: unliked-response-img

如何只制作 posts.save()posts.save().exec() 而不是创建一个新的 "copied" post 并保存它。