简体   繁体   中英

For loop iterating over array in NodeJS but console.log won't print to terminal?

I'm using Node.JS to iterate through some data and push that data to an array. However, console.log does not seem to show any changes that I've made. I'd like to be able to process the data in twitter_ids after the function is done pushing data to it.

I'm wondering if it's due to a misunderstanding of Node.JS's asynchronous nature?

var twitter_ids = []

function sendResults (twitter_ids){
    return function(data){
        for (var num in data['results']) {
            T.get('users/lookup', { screen_name: data['results'][num]['twitter_id'] }, function(err, data, response) {
                twitter_ids.push(data[0]['id']);
            });
        }
    console.log(twitter_ids);
    }
}

sunlightResults.call(sendResults(twitter_ids));

Your problem is that you are printing to the console before T.get() has retrieved any data.

If you need to wait until multiple callbacks have been called (as per your example), I usually use a helper library function like async.eachSeries(). If you want to do it yourself, something like recursion can be your friend, but might be a little convoluted:

function lookup(list, index, output, finished) {
    if(index >= list.length) { return finished(output); }

    var num = list[index];
    T.get('users/lookup', { screen_name: data['results'][num]['twitter_id'] }, function(err, data, response) {
        output.push(data[0]['id']);
        lookup(list, index+1, output, callback);            
    });
}

var outputList = [];
lookup(data['results'], 0, outputList, function(output){
    console.log(output);
});

I am sure some genius here can make this better an more readable, but just a super quick example.

Here's an implementation using async

var async = require('async');
var twitter_ids = []

function sendResults (twitter_ids){
    return function(data){
        async.each(data.results, function (result, done) {
            T.get('users/lookup', { 
                screen_name: result.twitter_id 
            }, function(err, data, response) {
                if (err) {
                    return done(err);
                }
                twitter_ids.push(data[0].id);
                done();
            });
        }, function (err) {
          if (err) {
              throw err;
          }
          console.log(twitter_ids);
        });
    }
}

sunlightResults.call(sendResults(twitter_ids));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM