Im having issues with accessing the value of a variable in NodeJS. Here is some example code and the results I get.
for (var z=0, zMessageCount = Description.length; z<zMessageCount; z++){
console.log(z);
if(SomeOtherColumnValue[z] > 9){
client.query('SELECT * FROM my_table WHERE some_column=' + ColumnValue[z], function (err, results) {
if(results < 1){
console.log(z);
}
})
}
}
Here is the issue I am having. In for "for" loop the value of z is going from 0 to 14. However when I try to access it from the client.query function its value is 15. It is not adding 1 to itself for every loop. Is there something I am missing here?
zshould be fine in that scope. Hmm, wild guess but what does client.query actually do with the string you pass it? I have a feeling it sends it to another non-blocking callback function and it's being executed after your loop finishes, rather than write away. What library is this?var columnName = ColumnName[z];and pass that into client.query. DefiningcolumnNameafter your if statement. This might help from the z being passed as a reference intoclient.query