I have tested this code below inside the snipped on the Google Chrome Console.
Input and Output:
var values = [{"id":"1","Product":"Pen","Qty":"10"},{"id":"2","Product":"Pencil","Qty":"20"}]
for(var key in values) {
var val = values[key];
console.log(val);
}
Using NODE JS
I have installed a module called body-parser. To access HTML content from Node JS.
// Get HTML values
var {values} = req.body;
// Iterate through the dictionary
for(var key in values) {
var value = values[key];
console.log(value);
}
This is the actual output on CMD - very different to the Google Chrome Console. I am confused to why it is different.
[
{
"
i
d
"
:
"
1
"
,
"
P
r
o
d
u
c
t
"
:
"
P
e
n
"
,
"
Q
t
y
"
:
"
1
0
"
}
,
{
"
i
d
"
:
"
2
"
,
"
P
r
o
d
u
c
t
"
:
"
P
e
n
c
i
l
"
,
"
Q
t
y
"
:
"
2
0
"
}
]
How can I get same output as Javascript console? I'd like to access it's id and other contents.
It's the same language (Javascript) but different outputs, how does what make sense? Is it because of using Node JS?
Edit:
Use var {values} = JSON.parse(req.body);
I get this error:
TypeError: Cannot convert object to primitive value
var {values} = JSON.parse(req.body);instead. Use this setup and it should happen automatically.TypeError: Cannot convert object to primitive valuewhen usingJSON.parse(req.body)console.log(req.body)?[Object: null prototype] { id: '1', Product: 'Pen', Qty '10', values: '[{"id":"1","Product":"Pen","Qty":"10"},{"id":"2","Product":"Pencil","Qty":"20"}]' }