Query sql nodejs loop

Asked

Viewed 96 times

-1

Hello I’m with a doubt I think they could help me, to some problem in running a function twice in the same script

example have this function

exports.getValueMsg = function(){
   return new Promise((resolve, reject) => {
        pconn.pool.query("SELECT b.numero_serie, b.grupo_msg, b.index_msg, b.valor_msg FROM  bor_msg b ", (err, rows) => {
            if (!err) {
               count = Object.keys(rows).length;
               while(i<count){
                  console.log(i);
                  dadosMsg = rows.rows[i];     
                  i++;
                  return resolve(dadosMsg);
               }       
                        
            } else {
                reject(err);
                console.log(err);
        }
    });  
   })
}

and it returns me the query results normally. More when I call this function ex below

  const exec = async() => { 
    try {
      var macroLength = await dbMsg.getValueMsg();
      var count = Object.keys(macroLength).length;
    
      for(var i=0;i<count;i++){
        var valueMsg = await dbMsg.getValueMsg();
        console.log(valueMsg);

        macro = await xmlTemplates.xml_16_begin(valueMsg);      
        
        clientOut.write(macro);

        await fc.wait(2200);

        clientOut.write(xmlTemplates.xml_16_end());

        await fc.wait(3000);
        
        dbMsg.upStatusMsg(statusMsg, valueMsg);
        
        setTimeout(function(){
          if(vDataOutObj.Package.Header._attributes.Id == 116){

            console.log(vDataOutObj.Package.Header._attributes.Id);

            dbMsg.upStatusMsg(statusMsg2, valueMsg);
         }
         
        },3100);
      }
  
    }catch (error) {
       console.log(error);
    }
  }

it returns me duplicated results and can not go through the loop fully, I think it is because I am running the function twice...so it duplicates the results and does not return me all.. but if I don’t perform the function getValueMsg() twice, as I will decide how far my For must travel

1 answer

0

The query would basically look like this by removing the repeat loop

exports.getValueMsg = function() {
    return new Promise((resolve, reject) => {
        pconn.pool.query("SELECT b.numero_serie, b.grupo_msg, b.index_msg, b.valor_msg FROM bor_msg b where b.id_tipo = '16'",
                (err, result) => {
                    if (result) {
                        resolve(result);
                    } else {
                        reject(err);
                    }
                }
            );  
   });
}

And the call to go through the query’s Results would look like this

const exec = async() => { 
    
    try {
      
      const queryResult = await dbMsg.getValueMsg();

      for(let i=0; i<queryResult.rows.length; i++) {
        
        const valueMsg = queryResult.rows[i]; 

        console.log(valueMsg);
       
       .......................
       
       ......................
       
      }
     
   }catch (error) {
       console.log(error);
    }
}

Browser other questions tagged

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