Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async.each nested in async.waterfall #658

Closed
maddy2308 opened this issue Nov 6, 2014 · 1 comment
Closed

async.each nested in async.waterfall #658

maddy2308 opened this issue Nov 6, 2014 · 1 comment
Labels

Comments

@maddy2308
Copy link

I have recently started using async api. Now my requirement is to perform a join on 3 collections namely fields, scripts and statements. fields can have multiple scripts, and scripts can have multiple statements.

here is what I have tried so far:(to join Fields collection with scripts)

// Array to hold async tasks
var asyncTasks = [];

async.waterfall([
function(callback){
// fetches fields based on some Id and it returns 2 fields
db.fields.find({entity_id: mongojs.ObjectId("54440a448bbbcbb4070131ab")}, function (err, fields) {
console.log(JSON.stringify(fields, null, 2));
callback(null, fields);
})
},
function(arg1, callback){
// arg1 now equals fields
arg1.forEach(function(eachField){
asyncTasks.push(function(callback){
db.scripts.find({fieldId: eachField._id.valueOf()}, function(err, scripts) {
// Async call is done then alert via callback
console.log(JSON.stringify(scripts, null, 2));
callback(null, scripts);
});
});
});

    // Now we have an array of functions doing async tasks
    // Execute all async tasks in the asyncTasks array
    async.parallel(asyncTasks, function(err, results) {
        // All tasks are done now
        console.log("Scripts" + JSON.stringify(results, null, 2));
        callback(null, "done");
    });

}

], function (err, result) {
console.log(result);
});
// for the above code here is what i get the output
[
{
"_id": "54440a548bbbcbb4070131ac",
"name": "t1",
"type": "String",
"entity_id": "54440a448bbbcbb4070131ab"
},
{
"_id": "54447f1d20c103981fa1a27c",
"name": "t2",
"type": "String",
"entity_id": "54440a448bbbcbb4070131ab"
}
]
size of array 2
[]
[]
Scripts[
[],
[]
]
done
The above output doesn't print any scripts even though there are 2 scripts in database. My database is is in MongoDB, and i am using NodeJs, MongoJS api. why is db.scripts.find() returning empty array?

I tested this piece of code to see if scripts returning the o/p. Please find below my code

test2();
function test2(){

    var getScriptFunction = function(eachField, doneCallback){
        if(eachField !== undefined) {
            var fieldId = eachField;
            console.log(fieldId);
            db.scripts.find({fieldId: fieldId}, function (err, result) {
                // Async call is done, alert via callback
                doneCallback(null, result);
            });
        }
    }
    // The array is the id of fields
    async.map(["54440a548bbbcbb4070131ac", "54447f1d20c103981fa1a27c"], getScriptFunction, function (err, results) {
        // Square has been called on each of the numbers
        // so we're now done!
        if (err){
            console.log("error!" + err);
        } else {
            console.log("printed from helper function \n" + JSON.stringify(results, null, 2));
        }
    });
}

This is the o/p of the above code to fetch scripts ran individually

printed from helper function
[
[
{
"_id": "54440a678bbbcbb4070131ad",
"name": "s1",
"fieldId": "54440a548bbbcbb4070131ac"
},
{
"_id": "544af260eb7a486824a5c306",
"name": "s2",
"fieldId": "54440a548bbbcbb4070131ac"
}
],
[]
]
This is how fields look like (db.fields.find().pretty())

[
{
"_id": "54440a548bbbcbb4070131ac",
"name": "t1",
"type": "String",
"entity_id": "54440a448bbbcbb4070131ab"
},
{
"_id": "54447f1d20c103981fa1a27c",
"name": "t2",
"type": "String",
"entity_id": "54440a448bbbcbb4070131ab"
}
]

@RyanCopley
Copy link
Contributor

I believe I've done this and am willing to help if you properly format your post to make it legible. Please include code tags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants