Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda