2017-01-27 39 views
0

請看看我的代碼。我有一個驗證錯誤,但我確信我將文檔放在正確的格式上。貓鼬驗證錯誤,但我把文檔正確

我的模型

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" 
} 

請參閱我的錯誤堆棧跟蹤 enter image description here

編輯:req.body enter image description here

什麼我這裏做錯了的結果?我卡住了。

+0

你在POST請求中發送了什麼?告訴我'console.log(req.body)'的含義。 –

+0

編輯我的問題以顯示請求正文的結果 –

+0

如何導入訂單?你將它們存儲在var Order或var order中。看到你使用過「Orders.create」。 – rresol

回答

1

您在創建新的order時犯了錯誤。看看req.bodycontents,看看你傳遞給orders的對象。

試試這個:

var orderItem = new Orders(); 
orderItem.userPurchased=body.userId; 
//for each products item, push it to orderItem.products 
body.products.forEach(function(product,index) 
{ 
    orderItem.products.push({ 
     product: product.product, 
     size: product.size, 
     quantity: product.quantity, 
     subTotal: product.subTotal 
    }); 
}); 
orderItem.totalQuantity=body.totalQuantity; 
orderItem.totalPrice=body.totalPrice; 
orderItem.otherShipAd=body.customAdd; 
orderItem.modeOfPayment=body.modeOfPayment; 

Orders.save(function (err, result) { 
    if (err) throw err; 
}); 
+0

我已經知道我的問題是將我的req.body中的項目數組推向varialble。這是我正在尋找的。順便說一下,forEach中索引參數的用法是什麼? –

+0

在這種情況下是可選的,您可以在此省略索引。只是'forEach(function(product){})'會起作用。 –

+0

如果它幫助你解決了你的問題,接受答案並且提高它。 –

1

你好像問一個單獨的後陣列同樣的問題 - 你應該避免重複你的問題。

答案更短 - >您不需要處理您的req.body,因爲它與您的模型所需的模式相匹配。這應該工作得很好:

ordersRouter.route('/placeOrder') 
    .post(function (req, res) { 

     Orders.create(req.body, function (err, result) { 
      if (err) throw err; 

      // send success message back 
     }); 
    }); 
+0

對不起。我想這裏的問題是我爲什麼要驗證錯誤,第二個問題是如何處理req.body中的對象數組。如果那會是重複的,那麼它就是我的錯。順便說一句,謝謝你。沒想到這也可以做到。我會試試這個,讓你知道。 –

+0

測試它,它的工作原理。我會高興。 –