[英]mongodb with mongoose; trouble updating 1 document in
感谢您的关注。
这是我的代码:
来自models / Cart.js
var mongoose = require('mongoose');
var CartSchema = mongoose.Schema({
owner: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
products: [ {type: mongoose.Schema.Types.ObjectId, ref: 'Product', quantity: Number} ],
created: Date,
updated: Date
});
exports.Cart = mongoose.model('Cart', CartSchema);
来自cart.js
var model = require('../models/Cart');
//modify a cart
router.post('/update/:id', function(req, res, next){
var cart = model.Cart();
console.log(cart);
cart.findByIdAndUpdate(req.params.id, {
products: req.body.products,
updated: moment().format()
}, {new:true}, function(err, result){
if(err) throw err;
res.status(200).send(result);
});
});
我得到的错误undefined is not a function
在cart.findByIdAndUpdate上undefined is not a function
,也不是将购物车日志记录为{ products: [], _id: 56b3b9fcd13a19a87bddd5f3 }
。
尝试通过删除或注释以下行: //var cart = model.Cart();
并将cart
替换为cart.findByIdAndUpdate(......)
行上的model
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.