1
 var robject=[]; 
 async.waterfall([
        function (callback) {
            for(var i in serial){
                Router.find({},{r_serial_no:serial[i]},function (err,routerData) {
                    robject = robject.concat(routerData);
                });
            }
            console.log('Robject= '+robject); //THIS RETURNS NULL
            callback(null, robject);
        },
        function (blogs, callback) {
            res.render('index', {dispatched_data:dispatched_data });
            callback(null, 'Ended..' );
        }

    ], function (err, result) {
        console.log(result);
    });

this is my waterfall model, here i need to access the robject from schema.find method to outside of that method. but it always return null.. how to access that??

5
  • First check are you getting anything in routerData ? It can be actually null. Commented Aug 9, 2017 at 5:54
  • no bro, inside the loop i get the data.. Commented Aug 9, 2017 at 5:58
  • Why would you project different fields from a query result in a loop? Why not do the query once and simply "loop the keys" to match the names in the array? Your problem here is without callback control the "loop" completes before the actual calls are made. But it's also completely unnecessary. Do the find() once and simply extract the values from the matching keys. Commented Aug 9, 2017 at 6:13
  • Moreover it actually looks like you really want $in here instead. i.e Router.find({ "r_serial_no": { "$in": serial } },function(err,routerData) {.. As you appear to want to "query" but you are putting the values in the "projection" part in error. Commented Aug 9, 2017 at 6:17
  • @Neil Lunn Bro.. u made my day.. thank you verry much.. actually i dont need a loop.. you are correct, i can put the array itself. Commented Aug 9, 2017 at 6:31

2 Answers 2

1

You have the syntax error:

 for(var i in serial){
                Router.find({},{r_serial_no: i},function (err,routerData) {
                    robject = robject.concat(routerData);
                });
            }

the "for" loop defines "i" as next item in the array each iteration

Sign up to request clarification or add additional context in comments.

3 Comments

bro that too works.. my problem is with accessing the variable. if i log inside the for loop i get the result.
try to remove all the conditions from the .find method, and fetch all results from db
bro thats not resolved the issue. still i cant access the variable
1

The problem I see here is in for...in loop. Your callback will be fired even if your process i.e. Router.find is not completed. You can try below code, It might help.

Unlike your serial object please create a array called serials.

var robject=[]; 
 async.waterfall([
        function (callback) {
            async.each(serials,
              function(serial, localCb){
                Router.find({},{r_serial_no:serial},function (err,routerData) {
                    robject = robject.concat(routerData);
                    localCb()
                });
              },
              function(err){
                console.log('Robject= '+robject); 
                callback(null, robject);
              }
          ); 
        },
        function (blogs, callback) {
            res.render('index', {dispatched_data:dispatched_data });
            callback(null, 'Ended..' );
        }

    ], function (err, result) {
        console.log(result);
    });

Comments

Your Answer

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