0

I have sent value in post request with postman to a nodejs api and now i want to get its values. If i write console.log(req.body); i get

{ 'vendor_medicine_id[]': [ '5b10dc0aa5d60c23a8947e7a', '5b0d4c9abcd16f0558afce85' ],
  'quantity[]': [ '15', '17' ],
  'name[]': [ 'abc', 'xyz' ],
  'contact_number[]': [ '0332695258', '44477889922225' ] }

i want to get this value '5b10dc0aa5d60c23a8947e7a', what should i write

console.log(req.body.???.???); 

can you please help

Sanaullah Ahmad
  • 474
  • 4
  • 12

2 Answers2

1

Since there are brackets in the key of the value you want to access, I'd recommend using bracket notation instead of dot notation. After that, you access the first item in the array by using [0].

console.log(req.body['vendor_medicine_id[]'][0]); 

If you want to access all values in that array, you might want to use an iteration:

req.body['vendor_medicine_id[]'].forEach(function (item) {
    console.log(item)
});

For more information, see: Loop through an array in JavaScript

Tom M
  • 2,815
  • 2
  • 20
  • 47
1

You can do this easily like this:

console.log(req.body["vendor_medicine_id[]"][0]);

Hope this helps.

Harshal Yeole
  • 4,812
  • 1
  • 21
  • 43