以下代码很早就被罚款,突然停止工作,现在我总是向收藏夹添加一个空文档。
{"_id":"5ccfca6bfbee8d4780d14cb0","__v":0,"product":[]}
我唯一得到的是:这里是模型
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var CartSchema = new Schema({
product: [{
productId: { type: mongoose.Schema.Types.ObjectId, ref: 'product' },
sku: {
type: String,
unique: true,
},
name: {
type: String,
required: true
},
brand: {
type: String,
required: true
},
category: {
type: String,
required: true
},
quantity: {
type: Number,
required: true
},
}]
})
module.exports = mongoose.model('Cart', CartSchema);
功能如下
router.post('/', function (req, res, next) {
console.log(req.body,"cart");
Cart.create(req.body, function (err, cart) {
if (err) {
return next(err);
}
res.json(cart);
});
});
要求正文如下
{productId: '5cd82b7c8308f629fc376fa1',
sku: 'dddd6',
name: 'G3 15 Gaming',
brand: 'Dell',
category: 'Laptop',
quantity: 1 }
router.post('/', function (req, res, next) {
console.log(req.body);
let cartInfo = new CartModel(req.body);
cartInfo .save(function(err,succ){
console.log("dddd====>>"+JSON.stringify(err))
if(err){
res.send({"responseCode":400,"responseMessage":"Cart is already exist."})
} else{
res.send({"responseCode":200,"responseMessage":"Cart information successfully added."})
}
})
使用.save功能保存数据... create函数创建一个空文档。