0

I have my node.js code like this

async.waterfall([
    function(callback){
        category = [{"id":1,"name":xxx},{"id":2,"name":yyy}];
        callback(null,category);
    },
    function(category,callback){
        var categoryData ={};
        _.each(category,function(item){
            categoryData[item.id] = item;        
            SolrClient.select(query,function(data){
                //data is the response from SolrClient
                categoryData[item.id]["data"] = data;
                log(categoryData);   //log1 
            },callback);
            log(categoryData);  //log2
        });
        log(categoryData); //log3
    }
]);

log1 => the data that I have added to categoryData is logged correctly
log2 => I cant get the data that is logged in the callback function to SolrClient
log3 => same as log2

I understand that this has something to do with variable scope OR that the callback to SolrClient cannot access the same categoryData initialized before _.each .

I have wasted a lot of time debugging it but i guess I am doing some small mistake which I am not getting how to solve.

1
  • what would really help you is proper indentation. Commented Dec 8, 2012 at 4:21

1 Answer 1

2

If you adjust your logs to include a unique message with each (such as the comments currently next to them), they probably appear in a particular, and perhaps unexpected, order:

log2
log2
log3
log1
log1

The issue you're experiencing isn't one of scope; it's one of timing. Functions can be called at any time -- not only immediately or synchronously as with _.each(), but also "later" or asynchronously as with SolrClient.select().

To ensure that all of the queries with SolrClient.select() finish before continuing, you can swap out _.each for async.forEach:

//...
function(category, waterfallCallback) {
    var categoryData = {};

    async.forEach(category, function (item, forEachCallback) {
        categoryData[item.id] = item;

        SolrClient.select(query, function (data) {
            categoryData[item.id]["data"] = data;
            forEachCallback(null);
        });
    }, function (err) {
        waterfallCallback(null, categoryData);
    });
}
//...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.