Duplicate Data Loop Query Nodejs

Asked

Viewed 74 times

0

I’m having a problem with a loop loop on node js, this is the query

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, rows) => {
                if (!err) {
                    while (i<rows.rows.length) {
                        dadosMsg = rows.rows[i]; 
                        i++
                        return resolve(dadosMsg);
                    }            
                } else {
                    reject(err);
                    console.log(err);
                }
            });  
   });
}

it returns me 5 results that comes from this select above, when I call her in this script

const exec = async() => { 
    try {
      var count = Object.keys(await dbMsg.getValueMsg()).length;

        for(i; i<count; i++) {
            valueMsg = await dbMsg.getValueMsg();
            console.log(valueMsg);

            configXml = await fc.dadosXml();      
            macro = await xmlTemplates.xml_16_begin(valueMsg, configXml);      
            clientOut.write(macro);

            await fc.wait(2200);

            clientOut.write(xmlTemplates.xml_16_end(valueMsg, configXml));

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

Instead of going through the results and bringing each one...it brings me repeated results and does not bring back all the results it has on query...

Obs(in my database table not to repeated records) someone can tell me what this could be and how to solve this my question ?

1 answer

1


It was not clear what the problem is. Analyzing the code we can improve some things and suddenly your problem can be solved:

  • Avoid using global variables as you did with i and dadosMsg.
  • Inside the loop for you are not assigning a value: for(i = 0; i < .... So your second loop will catch the final value of the variable i referring to the last execution of the previous loop.
  • The variable dadosMsg is expendable

See if the section below helps:

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 (err) {
                        reject(err);
                    } else {
                        resolve(result);
                    }
                }
            );  
   });
}


const exec = async() => {
    const queryResult = await dbMsg.getValueMsg();
    for(let i = 0; i < queryResult.length; i++) {
        const item = queryResult[i];
        console.log(`Item ${i+1}:`, item);

        // ...
        // ...
    }
}   
  • It was basically that, I made some adjustments and now it’s working the right way... Thank you very much Dude

Browser other questions tagged

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