中的1個文檔時出現問題謝謝您的關注。mongodb與貓鼬;更新
這裏是我的代碼:
從型號/ 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
就在購物車。 findByIdAndUpdate和購物車日誌爲{ products: [], _id: 56b3b9fcd13a19a87bddd5f3 }
。
你的'/models/Cart.js'是怎麼樣的?將其代碼也包含在問題中! – narainsagar