Reputation: 119
Trying to do a request through request-promise but it's not working properly.
See the comment below.
Can somebody explain this PiA to me?
Thanks in advance
var todayOptions = { uri: `http://mlb.mlb.com/gdcross/components/game/mlb/year_${year}/month_${month}/day_${day}/master_scoreboard.json`,
simple: false,
resolveWithFullResponse: true
};
rp(todayOptions)
.then(function (response){
console.log(response.body); //RETURNS BODY
console.log(response.body.data); //RETURNS UNDEFINED EVEN THOUGH IT EXISTS
})
.catch(function(error){
console.log(error);
});
Upvotes: 0
Views: 3956
Reputation: 1468
If this still doesn't work for you, as it didn't for me, try adding this to your options:
encoding: 'utf8'
That fixed everything for me. You don't even have to mess with json.parse or json.stringify compounding. The response is already a json object if you already have json: true in your options.
Upvotes: 0
Reputation: 29172
You need set json
option to true:
var todayOptions = { uri: `http://mlb.mlb.com/gdcross/components/game/mlb/year_${year}/month_${month}/day_${day}/master_scoreboard.json`,
simple: false,
resolveWithFullResponse: true,
json: true
};
Upvotes: 4
Reputation: 5645
My bet is response.body
is a stringified JSON object. Try parsing it. request-promise returns a stringified object sometimes.
console.log(JSON.parse(response.body).data);
Upvotes: 3