nero nero - 1 year ago 74
Node.js Question

how to get a specific data value from a body?

I'm using the "request" npm to send POST , PUT , Get and Delete requests to my API.

how can I get the value of a specific data in the body ?

example : I want to get the value of _id and store it

body: '{"_id":"58332517c4afbd230074732e","PushToken":"1","AuthCode":"1","DeviceID":"1"}'

my current get request is

request.get('http://localhost:3000/api/pass/58332517c4afbd230074732e', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log( body._id);
console.log( response.body._id);

I tried both,
they both return

Answer Source

Returned body is a JSON string, which you need to parse it into an object first.

var data = JSON.parse(body);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download