2017-01-27 5 views
0

私のコードを見てください。私は検証エラーが発生していますが、私は正しい形式で私の文書を置くことを確信しています。マングース検証エラーですが、文書を正しく入れました

POSTMAN FROM MY MODEL

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); 

MY ROUTE

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

? 'console.log(req.body)'のrsultを教えてください。 –

+0

リクエストボディの結果を表示するために私の質問を編集しました –

+0

注文をどのようにインポートしていますか?それらをvar Orderまたはvar orderに格納していますか? "Orders.create"を使ってください。 – rresol

答えて

1

新しいorderを作成中に間違っています。 contentsreq.bodyを見て、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からの項目の配列をバリエールにプッシュしていることを知っています。これは私が探しているものです。ところで、forEachのインデックスパラメタの使い方は?この場合にはオプションの –

+0

を指定すると、ここでindexを省略できます。 'forEach(function(product){})'だけが動作します。 –

+0

問題を解決するのに役立ちましたら、回答を受け入れ、アップアップしてください。 –

1

あなたは別のポストの配列について同じ質問をしているように見える - あなたはあなたの質問の重複を避ける必要があります。

答えが短くても、req.bodyがモデルの必要なスキーマと一致するため、req.bodyを処理する必要はありません。これは、正常に動作する必要があります:あなたはPOSTリクエストで送信しているもの

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

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

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

申し訳ありません。私はここで私の質問は、なぜ私は検証エラーがあると思う、と2番目にreq.bodyのオブジェクトの配列を処理する方法です。それが重複しているなら、それは私のせいです。ところで、これに感謝します。これもやっているとは思わなかった。私はこれを試み、あなたに知らせるでしょう。 –

+0

がそれをテストして動作します。私はupvoteよ。 –

関連する問題