Nodejs - Express - Mongoose:PUT使用save方法请求问题

时间:2017-08-04 11:00:56

标签: javascript node.js mongodb express mongoose

当我发出PUT请求时,

节点向我显示此错误:

TypeError: product.save is not a function

(指 controllers / product.js中的save功能

我发现这是更新文档的正确形式,但我遇到了这个问题。

我正在分享部分代码:

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

模型/ 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);

控制器/ 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);
    });
});
};

4 个答案:

答案 0 :(得分:1)

因为Model.find返回一个项目数组,而不是特定的猫鼬对象。因此,请尝试Model.findOneModel.findById(id,(err,data)={})

然后你可以调用save函数。还有另一种方式,而不是两个查询尝试一个。 Model.findOneAndUpdate()

希望这有帮助。

答案 1 :(得分:1)

Preferably, use mongoose findOneAndUpdate method for your update like this. 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'})
    });
};

答案 2 :(得分:0)

这样做

std::string

答案 3 :(得分:-1)

请使用此

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

而不是

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

);
});
};

这意味着您需要将产品用作产品