mongoose $ inc返回" undefined不是函数"

时间:2015-06-29 10:08:37

标签: javascript node.js mongodb mongoose

我正在尝试使用NodeJS和MongoDB(使用Mongoose ORM)进行切换。

当我找到find()的特定帖子时,我将其传递给以下函数,该函数在Like集合中添加新数据。

代码如下所示:

...
Post.findOne({
    _id:req.params.post_id
}, function(err, post){
    if(err){
        return res.json({
            type:false, 
            message:"error finding post"
        });
    }else if(!post){
        return res.json({
            type:false, 
            message:"there is no post with the id"
        });         
    }else{
        callback(null, post); //returns post instance
    }
});

... //skipped because the code is too long

var like = new Like({
    post_id: req.params.post_id,
    user_id: req.user.id
});         
console.log(result);
like.save(function(err, updated_like){ //save the like data first
    if(err){ 
        return res.json({
            type:false,
            message:"could not save like"
        });
    } else {
        //trying to update post with the new data
        post.likes.$inc(); // PROBLEM HERE
        post.like_user_id.push(req.user.id); //no problem with this
        post.save(function(err){
            if(err){
                return res.json({
                    type:false, 
                    message:"could not like"
                });
            } 
            return res.json({
                type:true, 
                message:"liked"
            });
        });
    }
});
...

我想在this页面的底部实施这种做法。

(示例代码):

Model.findOne({ name: 'borne' }, function (err, doc){
  doc.name = 'jason borne';
  doc.visits.$inc();
  doc.save();
});

我的代码看起来基本相同,但它给出了以下错误: (like_ctrl.js:90:18是问题出现的地方!)

or: undefined is not a function
or: undefined is not a function
EventEmitter.<anonymous> (C:\Users\NI\projects\pook\app\controllers\like_ctrl.js:90:18) 
EventEmitter.<anonymous> (C:\Users\NI\projects\pook\node_modules\mongoose\node_modules\mpromi
promise.js:175:45)
EventEmitter.emit (events.js:110:17)
Promise.safeEmit (C:\Users\NI\projects\pook\node_modules\mongoose\node_modules\mpromise\lib\p
js:81:21)
Promise.fulfill (C:\Users\NI\projects\pook\node_modules\mongoose\node_modules\mpromise\lib\pr
s:94:24)
Promise.resolve (C:\Users\NI\projects\pook\node_modules\mongoose\lib\promise.js:113:23)
model.<anonymous> (C:\Users\NI\projects\pook\node_modules\mongoose\lib\document.js:1569:39)
next_ (C:\Users\NI\projects\pook\node_modules\mongoose\node_modules\hooks-fixed\hooks.js:89:3

EventEmitter.fnWrapper (C:\Users\NI\projects\pook\node_modules\mongoose\node_modules\hooks-fi
ks.js:171:15)
EventEmitter.<anonymous> (C:\Users\NI\projects\pook\node_modules\mongoose\node_modules\mpromi
promise.js:175:45)
EventEmitter.emit (events.js:110:17)
Promise.safeEmit (C:\Users\NI\projects\pook\node_modules\mongoose\node_modules\mpromise\lib\p
js:81:21)
Promise.fulfill (C:\Users\NI\projects\pook\node_modules\mongoose\node_modules\mpromise\lib\pr
s:94:24)
p1.then.then.self.isNew (C:\Users\NI\projects\pook\node_modules\mongoose\lib\model.js:254:27)

newTickHandler (C:\Users\NI\projects\pook\node_modules\mongoose\node_modules\mpromise\lib\pro
:229:18)
process._tickCallback (node.js:355:11)
=============

我在这里做错了什么?

添加到此,

我的帖子架构看起来像

var postSchema  = new Schema({
    title: {type:String},
    content: {type:String},
    likes:{type:Number, default:0},
    like_user_id:[{type:Schema.ObjectId, ref:'Like'}]
});

这个和类似的架构如下所示:

var likeSchema  = new Schema({
    user_id: Schema.ObjectId,
    post_id: {type:Schema.ObjectId, ref:'Post'},
    created_at:{type:Date, default:Date.now}
});

1 个答案:

答案 0 :(得分:1)

问题在于,我正在查看Mongoose 2.7文档,并且在Mongoose 4中不再支持返回实例上的$ inc原型。

但是,新文档中没有替代$ inc的相关代码。

所以我找到了自己的方式,解决方案非常简单。

只需post.likes++;代替post.likes.$inc();