Mongoose validation error but I put documents correctly

后端 未结 2 928
说谎
说谎 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;
    });
    
    0 讨论(0)
  • 2021-01-28 01:10

    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
            });
        });
    
    0 讨论(0)
提交回复
热议问题