Please take a look at my code. I am having a validation error but I am quite sure that I put my documents on the correct format.
MY MODEL
You are doing mistake while creating a neworder
. Look at the contents
of req.body
and look what you are passing to the orders
object.
Try this:
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;
});
You seem to have asked the same question about the array in a separate post - you should avoid duplicating your questions.
The answer is shorter -> you have no need of processing your req.body as it matches the required schema of your model. This should work fine:
ordersRouter.route('/placeOrder')
.post(function (req, res) {
Orders.create(req.body, function (err, result) {
if (err) throw err;
// send success message back
});
});