Mongoose validation error but I put documents correctly

后端 未结 2 929
说谎
说谎 2021-01-28 00:11

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



        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-28 01:02

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

提交回复
热议问题