简体   繁体   中英

mongoose $inc returns “undefined is not a function”

I am trying to make a like toggle using NodeJS and MongoDB(with Mongoose ORM).

When I find the specific post with find() , I passed it to following function which adds a new data in the Like collection.

The code looks like below:

...
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"
            });
        });
    }
});
...

I am tring to implement the practice at the bottom of this page.

(The example code):

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

My code basically looks the same, but it gives the error below: (like_ctrl.js:90:18 is where the problem is occuring!)

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)
=============

What did i do wrong here?

add to this,

I have post schema which looks like

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

this and like schema looks like below:

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

The problem was that I was looking at Mongoose 2.7 docs and $inc prototype on the returned instance is no longer supported in Mongoose 4.

However, there was no relevant code in the new documentation that replaces the $inc.

So I found my own way and the solution was shockingly simple.

just do post.likes++; instead of post.likes.$inc();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM