0

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)

1565986223
  • 6,420
  • 2
  • 20
  • 33
WikeC
  • 131
  • 2
  • 10
  • THis SO post may help (https://stackoverflow.com/questions/31775177/nodejs-and-mysql-wait-for-query-result) – Ryan Wilson Apr 19 '19 at 12:25
  • `console.log` runs before the callback in your `query` is called, which is why you don't have the result. – 1565986223 Apr 19 '19 at 12:35
  • You can put the last `console.log(log);` in a separate function and call this function when log is completely filled. Suppose the separate function is called `doConsoleLog()`, then at the end of the callback-function of connectionQuery, you can write `if (log.length == result.length*2) doConsoleLog();` – www.admiraalit.nl Apr 19 '19 at 12:41

1 Answers1

0

First it seems you populate your array twice

store.action = "excel Valid"; 
log.push(store)

and later in your query callback

if (results1.length > 0)
{
 store.state = "update user";
 log.push(store)
}
// user create
else
{
 store.state = "create user";
 log.push(store)
}

according to this you should have a var log like this :

[ { action: 'excel Valid' },
  { action: 'excel Valid' },
  { action: 'excel Valid' },
  { action: 'excel Valid' },
  { action: 'excel Valid' } 
  { action: 'update user' } 
  { action: 'update user' } 
  { action: 'create user' } 
  { action: 'create user' } 
  { action: 'create user' } ]

But no, so it make me think the callback is not launching or not returning the expected format nor values, so can we see the query response?

Nil
  • 355
  • 1
  • 2
  • 8
  • 2
    I think you are right that he populates the array twice, but the `console.log(log)` at the end is outside of the callbacks, and therefore will be executed before the callbacks are made. – 0xnoob Apr 19 '19 at 12:36