简体   繁体   English

Node.js-Express-猫鼬:保存方法出现PUT请求问题

[英]Nodejs - Express - Mongoose: PUT Request problems with save method

Node shows me this error when I make a PUT request: 当我发出PUT请求时,Node向我显示此错误:

TypeError: product.save is not a function

(referring to the save function in controllers/product.js ) (请参阅controllers / product.js中save函数)

I find this is the right form to update a document, but I have this problem. 我发现这是更新文档的正确形式,但是我有这个问题。

I'm sharing part of the code: 我正在共享部分代码:

app.js app.js

var api = express.Router();

api.route('/products') 
 .get(ProductCtrl.findAll)
 .post(ProductCtrl.add);

api.route('/products/:isbn')
 .get(ProductCtrl.findByISBN)
 .delete(ProductCtrl.delete)
 .put(ProductCtrl.update);

app.use('/api', api);

models/product.js 车型/ product.js

var mongoose = require('mongoose');
var Schema = mongoose.schema;

var productSchema = mongoose.Schema({
  isbn: {type: String},
  title: {type: String},
  author: {type: String},
  template: {type: String},
  active: {type: Number} //1 - Active, 0 - Inactive
});

module.exports = mongoose.model('Product', productSchema);

controllers/product.js 控制器/ product.js

//PUT - Update the product by ISBN
exports.update = function(req, res){

Product.find(req.params.isbn, function(err, product){
    if(!product) res.status(404).send({message: 'Product not exits'});
    console.log("PUT - /products/" + req.params.isbn);
    product.isbn = req.body.isbn;
    product.title = req.body.title;
    product.author = req.body.author;
    product.active = req.body.active;
    product.template = req.body.template;

    product.save(function(err){
        if(err) return res.send(500,err.message);
        console.log("Successfully updated: " + req.body.isbn);
        res.status(200).json(product);
    });
});
};

Its because Model.find returns an array of items, not a specific mongoose object. 这是因为Model.find返回项目数组,而不是特定的猫鼬对象。 So either try Model.findOne or Model.findById(id,(err,data)={}) 因此,请尝试Model.findOneModel.findById(id,(err,data)={})

Then you can call the save function. 然后,您可以调用save功能。 Also there's another way, instead of two queries try one. 还有另一种方法,而不是两个查询尝试一个。 Model.findOneAndUpdate()

Hope this helps. 希望这可以帮助。

Preferably, use mongoose findOneAndUpdate method for your update like this. 最好这样使用findOneAndUpdate方法进行更新。 It looks terse and cleaner this way. 这样看起来简洁明了。

exports.update = function(req, res){

  Product
    .findOneAndUpdate({ isbn: req.params.isbn }, req.body)
    .exec(function(err, product){
      if(err) return res.status(500).json({err: err.message}):
      res.json({product, message: 'Successfully updated'})
    });
};

Do this 做这个

Product.find(req.params.isbn, function(err, product){
    if(!product) res.status(404).send({message: 'Product not exits'});
    console.log("PUT - /products/" + req.params.isbn);
    product.isbn = req.body.isbn;
    product.title = req.body.title;
    product.author = req.body.author;
    product.active = req.body.active;
    product.template = req.body.template;

   let updateproduct = new Product(product);

    updateproduct.save(function(err){
        if(err) return res.send(500,err.message);
        console.log("Successfully updated: " + req.body.isbn);
        res.status(200).json(product);
    });
});
};

Please use this 请用这个

exports.update = function(req, res){

    Product.find(req.params.isbn, function(err, product){
        if(!product) res.status(404).send({message: 'Product not exits'});
        console.log("PUT - /products/" + req.params.isbn);
        product.isbn = req.body.isbn;
        product.title = req.body.title;
        product.author = req.body.author;
        product.active = req.body.active;
        product.template = req.body.template;

        Product.save(product,function(err,data){
            if(err) return res.send(500,err.message);
            console.log("Successfully updated: " + req.body.isbn);
            res.status(200).json(product);
        });
    });
    };

instead of 代替

    exports.update = function(req, res){

    Product.find(req.params.isbn, function(err, product){
        if(!product) res.status(404).send({message: 'Product not exits'});
        console.log("PUT - /products/" + req.params.isbn);
        product.isbn = req.body.isbn;
        product.title = req.body.title;
        product.author = req.body.author;
        product.active = req.body.active;
        product.template = req.body.template;

        product.save(function(err){
            if(err) return res.send(500,err.message);
            console.log("Successfully updated: " + req.body.isbn);
            res.status(200).json(product);
        }

);
});
};

That means you need to use product as Product . 这意味着您需要将product用作Product

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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