0

I have spend hours trying to revolve this but I can not.

function get(req, res) {        
    let results = {};    
    req.body.vehiculos.forEach(async vehiculo => {
        const response = await axios.get(`https://api/foo=${vehiculo}`)                

            response.data.data.forEach(async truck => {
                results[truck.id] = [];
                let queryresult = await sql.query(`SELECT x from x where = ${vehiculo}`)
                results[truck.id].push({ ...truck, ...queryresult.recordset[0] });
                console.log('------------------------------');
                console.log(results);

            });                        
    })    
    return res.status(200).send({ message: "Success", data: results })
}

This is my JavaScript function I need to fill the results variable with the data that the axios returns, I have the data but can not access to the results variable, I can read it but can not rewrite, hope you can help me, I am new in JavaScript. Greetings.

Mexa Fireg
  • 83
  • 2
  • 9

1 Answers1

0

Thank you so much @K0pernikus, you saved my day.

This is the code after his magic.

async function get(req, res) {        
    let results = {};    
    for(let vehiculo of req.body.vehiculos)
    {
        const response = await axios.get(`https://api/foo=${vehiculo}`)                
        for(let truck of response.data.data){            
            results[truck.id] = [];
            let queryresult = await sql.query(`SELECT x from x where x = ${vehiculo}`)
            results[truck.id].push({ ...truck, ...queryresult.recordset[0] });
            
        }
    }
    
    return res.status(200).send({ message: "Success", data: results })
}

hope it helps someone else.

Mexa Fireg
  • 83
  • 2
  • 9
  • This still needed error handling. What if the HTTP call fails? What if the DB query fails? What if it no data coming in? – Janos Vinceller Aug 18 '20 at 04:34
  • yes it is already fixed, I only was having problems with that part, but after that it was easy, thank you @JanosVinceller – Mexa Fireg Aug 18 '20 at 20:06