Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda