請看看我的代碼。我有一個驗證錯誤,但我確信我將文檔放在正確的格式上。貓鼬驗證錯誤,但我把文檔正確
我的模型
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var orderSchema = new Schema({
userPurchased: { type: Schema.Types.ObjectId, ref: 'users' },
products: [
{
product: { type: Schema.Types.ObjectId, ref: 'products' },
size: { type: String, required: true },
quantity: { type: Number, required: true },
subTotal: { type: Number, required: true }
}
],
totalQuantity: { type: Number },
totalPrice: { type: Number },
otherShipAd: { type: String },
modeOfPayment: { type: String },
paidStatus: {type: Boolean, default: false}
});
module.exports = mongoose.model('orders', orderSchema);
我的路線
ordersRouter.route('/placeOrder')
.post(function (req, res) {
var body = req.body;
console.log(req.body);
var orderItem = {
userPurchased: body.userId,
products: [{
product: body._id,
size: body.size,
quantity: body.quantity,
subTotal: body.subTotal
}],
totalQuantity: body.totalQuantity,
totalPrice: body.totalPrice,
otherShipAd: body.customAdd,
modeOfPayment: body.modeOfPayment
};
Orders.create(orderItem, function (err, result) {
if (err) throw err;
});
});
MY JSON對象從郵差
{
"userPurchased": "5887f303c58a953360fe2759",
"products": [{
"product": "58466e8e734d1d2b0ceeae00",
"size": "m",
"quantity": 3,
"subTotal": 1197
},
{
"product": "58466e8e734d1d2b0ceeae00",
"size": "l",
"quantity": 3,
"subTotal": 1197
}],
"totalQuantity": 6,
"totalPrice": 2394,
"otherShipAd": "",
"modeOfPayment": "BDO"
}
什麼我這裏做錯了的結果?我卡住了。
你在POST請求中發送了什麼?告訴我'console.log(req.body)'的含義。 –
編輯我的問題以顯示請求正文的結果 –
如何導入訂單?你將它們存儲在var Order或var order中。看到你使用過「Orders.create」。 – rresol