0

In postman, I have done in form-data KEY -products VALUE-

[
  { name: "Pizza", price: "10", quantity: "7" },
  { name: "Cerveja", price: "12", quantity: "5" },
  { name: "Hamburguer", price: "10", quantity: "2" },
  { name: "Fraldas", price: "6", quantity: "2" },
];

In code,I want to receive this same array of objects but I can receive it as a string. My code is

var products =req.body.products;
console.log(typeof(products))//string shows
var Products=JSON.parse(JSON.stringify(products))
console.log(typeof(Products))// it also string shows

if I print array 0 index value it prints "[" array bracket Please Help me out I am new in this. Thank you

DecPK
  • 24,537
  • 6
  • 26
  • 42

2 Answers2

0

I think you have ask duplicate question Click here! that solution worked for me!

0

Since products is a string, you only need to parse this into an object with JSON.parse()

Use JSON.stringify() when u want to convert a javascript object to a string.

So:

var products = JSON.parse(req.body.products); // parse string to object
console.log(products[0]); // will output the pizza
Rappee_
  • 86
  • 2