Search code examples
node.jsmongodbexpressmongoskin

TypeError: Object #<Object> has no method 'save'


I keep getting the aforementioned error when I try to do a PUT request to update (error occurs at the line "item.save(function (err) ..." . Below is my PUT request code:

outer.put('/alltweets/:id', function(req, res){
    var db = req.db; 
    db.collection('tweetdb').findById(req.params.id, function (err, item){
        if (err) 
            res.send(err); 
        item.tweet = req.body.tweet; 
        item.date = req.body.date; 

        //save the item 
        item.save(function(err) {
            if(err) 
                res.send(err); 

            res.json({message: 'item updated' }); 
        }); 
    });     
}); 

Currently using Node, Express, and MongoDB.


Solution

  • Did some research, and finally realized save method is actually non existent in Mongoskin. Instead they use update (particularly updateById in the newer versions). PUT request works now with the code below (for Mongoskin):

    router.put('/alltweets/:id', function(req, res){
        var db = req.db; 
        db.collection('tweetdb').updateById(req.params.id, {$set:req.body}, {safe: true, multi: false}, function(e, result){
    
        if (e) return next (e); 
        res.send((result===1)?{msg:'success'}:{msg:'error'})
    
        }); 
    });