0

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
10
  • 1
    It's because in node, you're splitting a string that happens to contain JSON into its characters. Use var {values} = JSON.parse(req.body); instead. Use this setup and it should happen automatically. Commented Jul 16, 2020 at 7:45
  • req.body is a string not an object, just do JSON.parse(req.body) beforehand Commented Jul 16, 2020 at 7:45
  • @ChrisG TypeError: Cannot convert object to primitive value when using JSON.parse(req.body) Commented Jul 16, 2020 at 7:47
  • What is the output of console.log(req.body)? Commented Jul 16, 2020 at 7:58
  • @ChrisG [Object: null prototype] { id: '1', Product: 'Pen', Qty '10', values: '[{"id":"1","Product":"Pen","Qty":"10"},{"id":"2","Product":"Pencil","Qty":"20"}]' } Commented Jul 16, 2020 at 8:21

1 Answer 1

1

req.body.values, from your comments, is a string. Parse it using JSON.parse like the following:

// Get HTML values
var values = JSON.parse(req.body.values);

// Iterate through the dictionary
for(var key in values) {
  var value = values[key];

  console.log(value);
}
Sign up to request clarification or add additional context in comments.

6 Comments

What is the console.log output?
The for loop does not work sorrt - but if I use this var {values} = JSON.parse(req.body.values); console.log(JSON.parse(req.body.values)); // Iterate through the dictionary for(var key in values) { var value = values[key]; } it shows my correct output
That's because it's an array. Change it to JSON.parse(req.body.values)[0]
still no output when using the for loop
But when I do this: ` var {values} = JSON.parse(req.body.values); console.log(values);` it fives me this output [Function: values]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.