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

227
Vistas
How to do complex logic in a Promise `then` clause? The request-promise syntax itself appears problematic

I'm running my javascript code with node.js and am using the request-promise library to generate https requests which return a json blob. So far, I've managed to send the request correctly--and I do receive the desired json object in the response, but I'm only seeing the keys, not the values. I want to extract the value for a key called "call_count". The problem I'm facing is the peculiar structure of the request-promise syntax, which appears to be preventing me from doing what I want to do.

Here's what the JSON response looks like when I run my query in Postman:

{
  "metric_data": {
    "from": "2021-12-12T23:56:12+00:00",
    "to": "2021-12-13T00:26:12+00:00",
    "metrics_not_found": [],
    "metrics_found": [
        "Mobile/Crash/All"
    ],
    "metrics": [
        {
            "name": "Mobile/Crash/All",
            "timeslices": [
                {
                    "from": "2021-12-12T23:53:00+00:00",
                    "to": "2021-12-13T00:23:00+00:00",
                    "values": {
                        "call_count": 0
                    }
                }
            ]
        }
    ]
  }
}

And here's my code that generates the request:

const rp = require ('request-promise');

let options = {
  method: 'GET',
  uri: 'https://api.crashCatcher.com/v2/mobile_applications/myAppID/metrics/data.json?names=Mobile/Crash/All&values[]=call_count&summarize=true',
  headers: {
    'User-Agent': 'request-promise',
    'X-Api-Key': 'MY API key goes here'
  },
  json: true // Automatically parses the JSON string in the response
};

rp(options).then(body => console.log(body)).catch(err => console.log(err.name, err.statusCode));

Currently, when I run this code, here's what I'm getting back:

{ metric_data: 
   { from: '2021-12-13T00:22:04+00:00',
     to: '2021-12-13T00:52:04+00:00',
     metrics_not_found: [],
     metrics_found: [ 'Mobile/Crash/All' ],
     metrics: [ [Object] ] } }

Now, I know I'm getting a JSON object back, and I know I need to do something like:

var myJson = JSON.parse(*body*);

And then something like:

console.log(myJson.metric_data.metrics[0].timeslices[0].values.call_count);

But when using the request-promise syntax, I don't know how to correctly assign what's coming back in my response body to a variable that I can then JSON.parse and extract the needed call_count value from.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You aren't limited to using arrow functions, and even arrow functions can have full function bodies.

For example:

rp(options).then(
    body => {
        let myJson = JSON.parse(body);
        console.log(myJson.metric_data.metrics[0].timeslices[0].values.call_count);
    }
).catch(err => console.log(err.name, err.statusCode));

or:

rp(options).then(
    function (body) {
        let myJson = JSON.parse(body)
        console.log(myJson.metric_data.metrics[0].timeslices[0].values.call_count)
    }
).catch(err => console.log(err.name, err.statusCode));

or even:

function handleResponse (body) {
    let myJson = JSON.parse(body)
    console.log(myJson.metric_data.metrics[0].timeslices[0].values.call_count)
}

rp(options).then(handleResponse).catch(err => console.log(err.name, err.statusCode));

or even:

const handleResponse = body => {
    let myJson = JSON.parse(body);
    console.log(myJson.metric_data.metrics[0].timeslices[0].values.call_count);
}

rp(options).then(handleResponse).catch(err => console.log(err.name, err.statusCode));

about 4 years ago · Juan Pablo Isaza Denunciar

0

So, the solution was to remove the json: true setting in my options. Once that was done, all solutions kindly listed by Inigo, worked! Thanks again for the help! This was a huge learning experience.

let options = {
  method: 'GET',
  uri: 'https://api.crashCatcher.com/v2/mobile_applications/960867101/metrics/data.json?names=Mobile/Crash/All&values[]=call_count&summarize=true',
  headers: {
    'User-Agent': 'request-promise',
    'X-Api-Key': 'My API key'
  }
};
about 4 years ago · Juan Pablo Isaza 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