How can i get loop value outside the loop. I explain my problem:
I have a loop. In this loop I make several requests. I would like each request to retrieve the value of the variables and then display everything at the end of my loop.
Here's an sample example of my code:
var log = [];
for (var i = 0; i < result.length; i++){
validateExcelJson(result[i], function(id) {
if(result[i].EMAIL != "") {
var query = "SELECT id as ID from Table_users where DELETED = 0 and LOWER(AES_DECRYPT(EMAIL, '"+cryptoKey+"')) = LOWER('" + result[i].EMAIL + "');";
var data = result[i];
store.action = "excel Valid";
log.push(store)
console.log("test")
connection.query(query, function (error, results1, fields) {
console.log("test 1")
if (error) throw error;
if (results1.length > 0)
{
store.state = "update user";
log.push(store)
}
// user create
else
{
store.state = "create user";
log.push(store)
}
});
}
});
}
console.log(log)
here's what I get:
test
test
test
test
[ { action: 'excel Valid' },
{ action: 'excel Valid' },
{ action: 'excel Valid' },
{ action: 'excel Valid' },
{ action: 'excel Valid' } ]
test 1
test 1
Unfortunately I do not have store values (store.state = "create user" / store.state = "update user"). What I would like to obtain is:
[ { action: 'excel Valid', state: 'update user' },
{ action: 'excel Valid', state: 'update user' },
{ action: 'excel Valid', state: 'create user' },
{ action: 'excel Valid', state: 'create user' },
{ action: 'excel Valid', state: 'create user' } ]
How can i do ? I think that has to do with asynchronous methods. If so, how to make synchronous? (with the promises? I'll admit it's very vague for me)