Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
request-promise returns undefined but exists

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);
  });
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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
};
over 4 years ago · Santiago Trujillo Report

0

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);

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!