ExpressJS + Mongoose:无法更新findByIdAndUpdate中的记录mongoose的功能

时间:2016-04-20 16:49:59

标签: node.js mongodb

我是JS,NodeJS,Mongodb的新手,我正试图找出我的问题。

exports.like = function (req, res, next) {
    PostModel.findByIdAndUpdate(req.params.postIdLike, req.body, {new : true},function (err, post) {
        if (err) {
            console.log(this.getErrorMessage(err));
            return next(err);
        } else {
            var numberPeopleLike = post.Like.PeopleLike.length;
            var currentUserID = req.body.CurrentUserID;
            console.log(numberPeopleLike);
            if (numberPeopleLike === 0) {
                post.Like.PeopleLike.push({id: currentUserID, flag: true});
                post.Like.NumberLike += 1;
                var data = {
                    value: post,
                    message: "Like",
                    status: 1
                };
                res.json(data);
                next();
            } else {
                // post like != null
                console.log("numberPeopleLike");
                for (var i = 0; i < numberPeopleLike; i++) {
                    if (post.Like.PeopleLike[i].id === currentUserID && post.Like.PeopleLike[i].flag === true) {
                        // user Liked
                        post.Like.NumberLike -= 1;
                        post.Like.PeopleLike[i].flag = false;
                        var data = {
                            value: post,
                            message: "Undo Like",
                            status: 0
                        };
                        res.json(data);
                        next();
                    } else {
                        // user don't like
                        post.Like.NumberLike += 1;
                        post.Like.PeopleLike.push({id: currentUserID, flag: true});
                        var data = {
                            value: post,
                            message: "Like",
                            status: 1
                        };
                        res.json(data);
                        next();
                    }

                }
            }

        }
    });
};

我的问题:

findByIdAndUpdate函数不是if(numberPeopleLike === 0)条件下的更新结果代码。虽然数据json具有值,但结果无法在集合上更新。怎么了?

[UPDATE] 当我用PUT方法实现执行函数(Update)时 data条件中if(numberPeopleLike === 0 )的值

{
  "value": {
    "_id": "5717938a857e9c8a0e71a512",
    "IDMemberProfile": "5711114c2b5ecdf11a23947d",
    "Title": "test",
    "Content": "test",
    "Status": 1,
    "__v": 0,
    "Like": {
      "NumberLike": 1,
      "PeopleLike": [
        {
          "id": "570f932dfc642a4f1df5b450",
          "flag": true,
          "_id": "5717a9e42ae3ca5f12180844"
        }
      ]
    },
    "PostTime": "2016-04-20T14:34:50.973Z"
  },
  "message": "Like",
  "status": 1
}

但是当我在集合中收到GET列表帖子时,此文档无法更新

{
      "_id": "5717938a857e9c8a0e71a512",
      "IDMemberProfile": "5711114c2b5ecdf11a23947d",
      "Title": "test",
      "Content": "test",
      "Status": 1,
      "__v": 0,
      "Like": {
        "NumberLike": 0,
        "PeopleLike": []
      },
      "PostTime": "2016-04-20T14:34:50.973Z"
    }

0 个答案:

没有答案