简体   繁体   中英

NodeJs parent function variable appears to be out of scope in async.waterfall

I've got this code:

function mainProcess(res) {  
async.waterfall([

        function(callback) {
            postExportDefinition(requestParams, callback);
        },

        function(requestParams, callback) {
            postSync(requestParams, callback);
        },

        function(requestParams, callback) {
            checkSync(requestParams, callback);
        },
        function(body, callback) {
            postUpdatedData(body, callback);
        },
        function(syncUri, callback) {
            getSyncResponseInIntervals(syncUri, callback);
        }
    ],

    // the bonus final callback function
    function(err, status) {

        if (err) {
            res.status(500)
                .send({
                    error: "Error (best handling ever)"
                });
            return;
        }
        if (ageOffset < 100){
            mainProcess();
        }else{
            res.send("Success: " + status);
            console.log(status);
            return;
        }
    });
 }

The res is undefined and I kind of understand why, but what is the right approach to be able to use res within the waterfall ?

One solution would be to pass the variable along in all the functions and all the callbacks, but that is not scalable at all and I hate such solution. Is it the only one?

Another solution would be to make res global for entire script, again, doesn't feel right.

EDIT: res is undefined only in the last recursion call, which I don't understand can it be something that res object has expired? I am using express framework and the last call has happened after 60s.

EDIT2:

It's all my fault, I was calling mainProcess also from one of the functions when age range wasn't suitable. That broke it. That was some legacy code that was left over.

unless there is an argument name res in the last function that is not shown or a new var res = delaration in the same function, res is visible via closure

The only way i see res being undefined is the code on

if (ageOffset < 100){
   mainProcess();
} 

executing which calls the mainProcess function recursively without passing the res variable, which on that call will be undefined

if you need the recursive call and the logic behind the code stands change it as follows

if (ageOffset < 100){
   mainProcess(res); // pass res again to mainProcess
}

The code looks absolutely fine. If you are getting res printed before waterfall & in waterfall's final callback its undefined means its getting changed in mid.

Ensure no where you are altering the res within any of the waterfall blocks or redeclaring as var res .

If you are still getting the issue, then debug it by each waterfall block level(Check for res in each block). So that which place its getting replaced you can get.

Update:

In the final callback atleast one time it should print res properly.

 function(err, status) {
   //Here it should print once at least
    console.log(res);
    if (err) {
    ...
    }
    if (ageOffset < 100){
        //here u r making res undefined for the next call. Since its not passed. If u pass it will still work
        mainProcess();
    }else{
        res.send("Success: " + status);
        console.log(status);
        return;
    }

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