Problem when executing a query in a repeat loop on the Mssql Node

Asked

Viewed 159 times

2

So,

I am using Node and mssql package (sql-server) to perform queries to my database.

however I am facing a difficulty to return queries that need to be executed within a repeat loop.

Maybe it’s something to do with the way I’m performing the callback. In other queries where I don’t need to perform a repeat loop, it usually works.

My controller:

api.contabilizacaoItems =  function (req, res, next) {
    itensParaEnvio = [];
    var dados = req.body;
    cont = 1
    console.log(dados.items.length);
    dados.items.forEach(function (item, key){ 
        item.aprovacao = dados.aprovacao
        solicitacaoSqlDAO.contabilizacaoItem(item, function (erro, recordset) {
           item.contItem= recordset.recordset;
           solicitacaoSqlDAO.aenItem(item, function (erro, recordset) {
               item.aenItens= recordset.recordset;
               itensParaEnvio[key]= item;
               if(cont == dados.items.length) res.status(200).json(itensParaEnvio);
               else res.status(404).json("erro");
           })
       })
    });  
};

return api;

My DAO

solicitacaoDAO.prototype.contabilizacaoItem = function (item, callback) {
  console.log("# PARA CONSULTA contabilizacaoItem# ");
  mssql.close();
  mssql.connect(this._connection, function (err) {
    if (err) {
      console.log("# ERRO AO REALIZAR CONEXAO PARA CONSULTA contabilizacaoItem# "+ err);
    }
    var request = new mssql.Request();
    query = "select * from alguma coisa ";
    // console.log(query);
    request.query(query, function (err, recordset) {
      if (err) {
        console.log(query);
        callback(err,recordset);
      } else {
        callback(err,recordset);
      }   
    });
  });
}

solicitacaoDAO.prototype.aenItem = function (item, callback) {
  console.log("# PARA CONSULTA aenItem# ");

  mssql.close();
  mssql.connect(this._connection, function (err) {
    if (err) {
      console.log("# ERRO AO REALIZAR CONEXAO PARA CONSULTA aenItem# "+ err);
    }

    var request = new mssql.Request();
    query = "select * from alguma coisa"
    // console.log(query);
    request.query(query, function (err, recordset) {
      if (err) {
        console.log(query);
        callback(err,recordset);
      } else {
        callback(err,recordset);
      }   
    });
  });
}

1 answer

0


Use promises to resolve only when all callbacks are finalized:

api.contabilizacaoItems = function(req, res, next) {
  var itensParaEnvio = [];
  var dados          = req.body;
  var promessas      = [];

  console.log(dados.items.length);

  dados.items.forEach(function(item, key) {
    promessas.push(new Promise(function(resolver, rejeitar) {
      item.aprovacao = dados.aprovacao;

      solicitacaoSqlDAO.contabilizacaoItem(item, function(erro, recordset) {
        item.contItem = recordset.recordset;

        solicitacaoSqlDAO.aenItem(item, function(erro, recordset) {
          item.aenItens       = recordset.recordset;
          itensParaEnvio[key] = item;

          resolver();
        })
      });
    }));
  });

  Promise.all([true, promessas]).then(function(values) {
    res.status(200).json(itensParaEnvio);
  }).catch(function() {
    res.status(404).json("erro");
  });
};
  • opa, vlw sorack, I’ll tesar later and I warn you

  • sorack, I’m trying to use it, but it still hangs on the first loop

  • @Cirostodulskideazevedo what the symptom?

  • so he executes the first time and passes without executing the next ones.. I see that it is promises that can solve but I don’t think I know how to implement.

  • 1

    I am having trouble in meo DAO, but I had another way to solve the problem, it was not the way I wanted but served at the time, thanks for the help!

Browser other questions tagged

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