1

I'm trying to gather tweets from twitter, on console if I choose 5 tweets to gather it displays every one of them but if I choose the mysql module to insert them into a database it inserts the last line 5 times.

Can't figure out how to resolve it.

Asking again, had no luck in the last 3 hours making this script work. Was pointed out to for loop does not change index inside function . Can someone please help me fix this problem?

for (var i = 0; i < data.length ; i++) { // get all the messages from username, devfined in search term
  var retweetId_str = data[i].id_str; // id_str = id not rounded more precise
  console.log('id_str:', retweetId_str); // 

 //id_str: 656087507604402176
 //id_str: 656063345192255488
 //id_str: 656063056947126272
 //id_str: 656062911530606592
 //id_str: 656062750574182400     
  con.query('SELECT retweetid_str FROM droid_retweets WHERE retweetId_str=?', retweetId_str, function(error,result){ 
    console.log('id_str:', retweetId_str);         

    //id_str: 656062750574182400
    //id_str: 656062750574182400
    //id_str: 656062750574182400
    //id_str: 656062750574182400
    //id_str: 656062750574182400
  }); 
} // for close
Community
  • 1
  • 1
Alex _TNT
  • 309
  • 1
  • 2
  • 12
  • 2
    check docs, query is probably an async method so by the time the queries run, retweetId_str is always 656062750574182400 – juvian Oct 19 '15 at 18:18

2 Answers2

1

It's because the for loop has executed all the iterations before you get response from con.query, that's why it prints the last one several times, because the index is at the last position

user3589262
  • 41
  • 1
  • 5
1

It's because of closures in javascript.

This answer should help explain it and there are a couple ways to handle it.

The easiest being to use .forEach()

data.forEach(function(d) { // get all the messages from username, devfined in search term

            var retweetId_str = d.id_str; // id_str = id not rounded more precise
            console.log('id_str:', retweetId_str); // 
           con.query('SELECT retweetid_str FROM droid_retweets WHERE retweetId_str=?', retweetId_str, function(error,result){ 
           console.log('id_str:', retweetId_str);         

          }); 

    }); // for close
Community
  • 1
  • 1
Catfish
  • 18,876
  • 54
  • 209
  • 353
  • It's not because of closures. It's because how variables are scoped. –  Oct 19 '15 at 18:23
  • Finally an example that works. Thank you very much. You don't know how grateful I am. – Alex _TNT Oct 19 '15 at 18:23
  • I'll add this as an answer in 4min, Thanks again – Alex _TNT Oct 19 '15 at 18:25
  • @Alex_TNT: The answer you were linked to in your previous question and here give this exact solution. –  Oct 19 '15 at 18:27
  • It might mean the exact solution for you that might be fluent in javascript but for me as a beginner and only experience I have is with html and 2d/3d graphic designing it's not the same solution. – Alex _TNT Oct 19 '15 at 18:31
  • `someArray.forEach(function(arrayElement) {` ... `data.forEach(function(d) {` Only thing different is variable names. –  Oct 19 '15 at 18:33
  • and I did not even tried forEach tried to implement what's above the line where's the response without success. – Alex _TNT Oct 19 '15 at 18:35
  • now after the example is in front of me I understand what I had to do but did not notice it and would have taken another 5 hours to make it work even if I did notice it. – Alex _TNT Oct 19 '15 at 18:38