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

118
Visualizações
How do I take data from multiple node-request bodies and pass into an EJS file

I am creating a website which uses an api to dispaly football (soccer) scores, fixtures etc. I have tried to create a global variable to save the request data to and then pass into ejs however this doesn't seem to work.

router.get('/stats', (req, res) => {


request(options, function (error, response, body) {
    if (error) throw new Error(error);

  top_scorer_data = JSON.parse(body)

});


request(options2, function (error, response, body) {
    if (error) throw new Error(error);

  top_assists_data = JSON.parse(body)

});


request(options3, function (error, response, body) {
    if (error) throw new Error(error);

  top_red_cards_data = JSON.parse(body)  

});


request(options4, function (error, response, body) {
    if (error) throw new Error(error);

  top_yellow_cards_data = JSON.parse(body)

});

res.render('bundesliga/stats', {})

})

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You should follow DRY principle and move request to a service or a helper function. You shouldn't use global variables, use promises instead, and wait for all requests to finish.

So, you should promisify request method (you should really migrate to axios or node-fetch, because request is deprecated)

and then use Promise.all to make all the requests with each option, and then gather results into variables, which you then pass to the .render method.

Try this:

router.get('/stats', async(req, res) => {

    function getData(options) {
        return new Promise((resolve, reject) => {
            request(options, function(error, response, body) {
                if (error) reject(error);
                resolve(JSON.parse(body));
            });
        });
    }



    const [top_scorer_data, top_assists_data, top_red_cards_data, top_yellow_cards_data] = await Promise.all([
        getData(options),
        getData(options2),
        getData(options3),
        getData(options4)
    ]).catch(err => {
        console.log('err', err);
    });

    res.render('bundesliga/stats', {
        top_scorer_data,
        top_assists_data,
        top_red_cards_data,
        top_yellow_cards_data
    });

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