Return of a Knex query using . map

Asked

Viewed 636 times

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);
  }
}

  • 1

    Instead of let response = await knexOra.raw(... forehead with return await knexOra.raw(....

  • 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.

  • You know how to use Promise.all?

2 answers

1


Some things c consider:

This method/function index does not seem to need feedback as you are sending the answer within it. In that case async is not necessary and you can treat everything "in the fashion of Precedents". Run the .map which returns Promises, and then passes this array of Promises to the Promise.all which will be called when all resolve. Then you can pass this array of responses to res.json(.

Example:

const knexOra = require("../database/oracle");

module.exports = {
  index(req, res) {
    const { info } = req.body;
    const queries = info.map((item) => {
      return 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]]
      );
    });

    Promise.all(queries)
      .then((responses) => res.status(200).json(responses))
      .catch((err) => console.log(err));
  },
};
  • 1

    I understood Sergio, I did not know this question of Promise.all(), I really appreciate the help and the explanation.

-1

Just use await as in const usuarios = await Usuarios.query()

users.map will work normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.