1
Good afternoon, you guys. I have a Controller that receives data from a frontend spreadsheet, read the dice and play these to run a query in the database, so my intention is to play the results back to the user. But I’m having trouble trying to perform the query within a . map using the Knex. The query is performed normally, the query receive the map values but the problem occurs when I try to " return " the values obtained in the searches for some variable or similar, because I can’t assign the value to anything of the type. In the .then
if I put a console.log(response)
it appears the values, even if in duplicate form. I have tried to give a .push
in Let test in there too or even a Function passing the variable by parameters, but nothing that I can return the value to the client in return res.json()
, because the variable arrives empty for the frontend. Does anyone know what I can do to be able to assign the value to something and send to the client ? NOTE: It is an Oracle bank.
mainController.js:
const knexOra = require('../database/oracle');
module.exports = {
async index(req, res) {
const { info } = req.body;
let teste = [];
let query = await info.map(async item => {
try {
let response = await knexOra.raw(`SELECT a.nrocheckout as NROPDV, a.nroempresa, c.numerodf as NROCUPOM, to_char(a.dtahoremissao, 'DD/MM/YYYY') as DTAEMISSAO, a.vlrtotal as Valor
FROM consincomonitor.tb_doctopagto a, consincomonitor.tb_docto b, mfl_doctofiscal c, mfl_dfitem d
WHERE a.seqdocto = b.seqdocto
and a.nroempresa = b.nroempresa
and a.nroempresa = c.nroempresa
and a.nroempresa = d.nroempresa
and b.nroempresa = c.nroempresa
and b.nroempresa = d.nroempresa
and a.nrocheckout = b.nrocheckout
and c.nroempresa = d.nroempresa
and c.nrocheckout = a.nrocheckout
and c.nrocheckout = b.nrocheckout
and a.nroformapagto = c.nroformapagto
and c.numerodf = d.numerodf
and c.seriedf = d.seriedf
and c.nroserieecf = d.nroserieecf
and to_char(a.dtahoremissao, 'DD/MM/YYYY') = to_char(c.dtahoremissao, 'DD/MM/YYYY')
and b.dtamovimento = c.dtamovimento
and a.codautorizacaotef = ?
and a.nroempresa = ?
GROUP BY a.nrocheckout, a.nroempresa, c.numerodf, a.vlrtotal, a.dtahoremissao
having sum(d.vlritem) = a.vlrtotal
`, [item[1], item[0]]).then(response => {
return response;
});
} catch (error) {
console.log(error);
}
});
console.log(query);
return res.status(200).json(teste);
}
}
Instead of
let response = await knexOra.raw(...
forehead withreturn await knexOra.raw(...
.– Sergio
Good afternoon @Sergio , I did this test earlier too, the result he has in the backend is a
Promise <Pending>
, only that as in a map appears 4 of these, which is the total of times it runs the query.– Leo
You know how to use
Promise.all
?– Sergio