mongoose:findByIdAndUpdate通过错误"消息":"转换为字符串失败的值\" undefined \"

时间:2015-08-01 14:56:58

标签: node.js mongodb mongoose

我有list REMOVE_ITEM型号,

Item

我想要做的是通过Id获取项目并更新:

var mongoose = require('mongoose');
var ItemSchema = new mongoose.Schema({
    title: {type: String, es_indexed:true},
    price: Number,
    description: {type: String, es_indexed:true},
    picture: String,
    vendor: {type: String, es_indexed:true},
    vendorId: String,
    vendorPicture: String,
    category: String
});
module.exports = mongoose.model('Item', ItemSchema);

但是,我得到了回应和错误,我无法弄清楚原因,这里是erorr:

app.put('/api/item/:id', ensureAuthenticated, function(req, res) {
    Item.findByIdAndUpdate(req.params.id, 
        {title: req.body.title, description: req.body.description, price: req.body.price},
        function(err, item) {
            if(err) {
                res.send(err);
                return;
            }
            res.send('product updated');
        });
})

1 个答案:

答案 0 :(得分:0)

在我看来,你并没有在req.body.title中获得任何东西,并且演员没有为undefined工作。但话又说回来,这太基础了,你可能已经检查过了,很难从代码片段中看出来。如果没有,我将从console.logging req.body.title开始,看看你接下来需要做什么......