Sending an array of objects in a node js post request

I am trying to send an array of objects in a post request but when I try to access the array with req.body it is just showing {object} and none of the objects content is being sent. Is there something wrong with my request?

here is my request:

Post http://localhost:3000/users/newOrder 
Content-Type: application/json

{
      "firstName": "Craig",
        "lastName": "Mar",
        "address1": "10 a st",
        "address2": "apt 2",
        "email": "c@yahhoo.com",
        "town": "red bank",
        "state": "nj",
        "zip": "07701",
        "shippingCost": 12.00,
        "orderCost": 25.00,
        "productsArray": [{
        "id": "4a482549-79aa-4798-8a3b-a19746cd645a",
        "title": "Microsoft Xbox X/S Wireless Controller Robot White",
        "price": 57,
        "quantity": 1,
        "total": "57.00",
        "image": "https://storage.googleapis.com/fir-auth-1c3bc.appspot.com/1692255251854-xbox.jpg"
    },
    {
        "id": "827ab3f1-b5d5-436a-93cc-7a4a28aefd3b",
        "title": "Sony WH-1000XM3 Bluetooth Wireless Over Ear Headphones with Mic (Silver)",
        "price": 773,
        "quantity": "2",
        "total": "1546.00",
        "image": "https://storage.googleapis.com/fir-auth-1c3bc.appspot.com/1692947383286-714WUJlhbLS._SL1500_.jpg"
    }
    
]

    }

a console log of the req.body is the following:

{
  firstName: 'Craig',
  lastName: 'Mar',
  address1: '10 a st',
  address2: 'apt 2',
  email: 'c@yahhoo.com',
  town: 'red bank',
  state: 'nj',
  zip: '07701',
  shippingCost: '12',
  orderCost: '25',
  productsArray: [ '[object Object]', '[object Object]' ]
}

if i try to access the values of the objects in the products array with dot notation i am getting undefined any ideas where i am going wrong? I have also tried a curl request but get the same result.

Couldn’t delete the post but figured out it was express validator messing it up here is my code I would like to use express validator to sanitize the request but can’t get it to work right. If anyone knows the proper parameters let me know i have tried both isArray and isObject but both are giving me validation errors for some reason.

exports.post_newOrder = [

  body("firstName")
    .trim()
    .isLength({ min: 1 })
    .escape()
    .withMessage("first name must be specified."),
  body("lastName")
    .trim()
    .isLength({ min: 1 })
    .escape()
    .withMessage("last name must be specified."),
  body("email")
    .trim()
    .isLength({ min: 1 })
    .escape()
    .withMessage("email must be specified."),
  body("address1")
    .trim()
    .isLength({ min: 1 })
    .escape()
    .withMessage("address 1 must be specified.")
    .escape(),
  body("address2")
    .trim()
    .escape(),
  body("town")
    .trim()
    .isLength({ min: 1 })
    .escape()
    .withMessage("town must be specified."),
  body("state")
    .trim()
    .isLength({ min: 1 })
    .escape()
    .withMessage("state must be specified."),
  body("zip")
    .trim()
    .isLength({ min: 1 })
    .escape()
    .withMessage("zip must be specified."),
  body("shippingCost")
    .trim()
    .isLength({ min: 1 })
    .escape()
    .withMessage("shipping cost must be specified."),
    body("productsArray")
    .isObject(),
    


  async function (req, res, next) {

    const errors = validationResult(req);
    if (!errors.isEmpty()) {

      res.json({
        data: req.body,
        errors: errors.array(),
      });
      return;
    }

    console.log(req.body)

    const order = new Order({
      firstName: req.body.firstName,
      lastName: req.body.lastName,
      address1: req.body.address1,
      address2: req.body.address2,
      email: req.body.email,
      town: req.body.town,
      state: req.body.state,
      zip: req.body.zip,
      shippingCost: req.body.shippingCost,
      orderCost: req.body.orderCost,
      productsArray: req.body.productsArray,
      quantity: req.body.quantity,
      shipped: false

    });
    try {
      await order.save()
      let allOrders = await Order.find().exec()
      res.status(200).json(allOrders)
    } catch (error) {
      res.status(500).json({ message: error });
    }



  }

]

Got it fixed error was from the database not the form validator

1 Like