Saturday, 10 June 2017

How to access key-value from JSON response

I was trying to get the sid and date_created from the JSON response of Twilio after sending a text message but they are always undefined.

I've also tried body.['sid'] and body.['date_created'] but they still don't return the correct values.

Code

const textMe = function sendSms() {
  request.post(options, (err, resp, body) => {
    if (err) {
      console.log('error:', err);
    } else {
      console.log('statusCode:', resp.statusCode);
      console.log('sid:', body.sid);
      console.log('dateCreated:', body.date_created);
    }
  });
};

textMe();

Expectation

statusCode: 201
sid: SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
dateCreated: Sat, 10 Jun 2017 14:55:14 +0000

Reality

statusCode: 201
sid: undefined
dateCreated: undefined



via sedawkgrep

No comments:

Post a Comment