mongo findAndUpdateOne在我的情况下失败了

mongo findAndUpdateOne failed in my case

本文关键字:失败 情况下 我的 findAndUpdateOne mongo      更新时间:2023-09-26

正在尝试执行restful更新。

我的型号.js

module.exports.updateGenre = function (id, genre, callback) {
    var query = {_id: id};
    var update = {
        name: genre.name
    }
    Genre.findOneAndUpdate(query, update, option, callback);
}

我的路线.js

app.put('/api/genres/:_id',function(req,res){
    var id = req.params._id;
    var genre = req.body;
    Genres.updateGenre(id, genre, null, function(err,genre){
        if(err){
            throw err;
        }
        res.json(genre)
    })
});

我得到了option is not define的错误。但我已经在路由中使用了null,我想知道这是个错误。除了null之外,我还尝试了{}。

只需将option参数添加到updateGenre函数中即可:

module.exports.updateGenre = function (id, genre, option, callback) {}

看起来您需要将option参数添加到模块的签名中。目前是:

module.exports.updateGenre = function (id, genre, callback) {

option添加到,使其看起来像:

module.exports.updateGenre = function (id, genre, option, callback) {