chran
chran

Reputation: 119

nodejs does not read JSON

This must be something very obvious but I spent enough time on it:

Node express gets a JSON object that was set in the browser. the object name is dddd.

when I:

console.log(req.params.dddd);

I get:

{"email":"[email protected]"} 

which is correctly what I set to node.js

but when I:

console.log(req.params.dddd.email);

I get undefined...

I must be missing the point on this one. Than you to anyone helping

Upvotes: 1

Views: 284

Answers (2)

Viddesh
Viddesh

Reputation: 441

try {
    var obj = JSON.parse( req.params.dddd);
} catch (e) {
    console.log("parsing error!!!")
}
console.log(obj.email)

Always use try ... catch block while using JSON.parse method.

Upvotes: 0

David Haim
David Haim

Reputation: 26536

I think you get a string and you mistakenly think it's a JSON. try JSON.parse before.

var obj = JSON.parse( req.params.dddd);
console.log(obj.email)

Upvotes: 6

Related Questions