Variable scope in javascript module

alweiss nginx-forum at forum.nginx.org
Tue Oct 30 16:07:50 UTC 2018


Hi Dmitry, thanks for your reply.

Here is my code called using js_content authorize; from a location {}
I want to say "if at least one subrequest answers HTTP/200, set the
final_status to 200 and at then end, return 200"

First thing is that i must declare requestCount with var. If i don't, i get
a "requestCount" is not defined 
I thought in javascript, you could declare a variable just using its name
with the "var" keyword to make it global.
Same for final_status if not declared with var. But using var, i'm not able
to update the final status from the "done" function.

I think i missing something in the hoisting behaviour of javascript …
Thanks !

request for REST answers 200 as well as OASS so we should "break" after the
first subrequest but final_status next to the subrequest is always evaluated
to 403 even if previous subrequest returned 200.
The only case were it works, it is if the last subrequest return 200 which
doesn't rely on final_status value.

CODE :

function authorize(req, res) {
    req.warn('Variables init ...');
    var n = 0;
    var final_status = 403;
    var servicesCodes = ['rest','oass'];
    var requestCount = servicesCodes.length;
    
    function done(reply) { //Callback for completed subrequests ...
        n++;
	    reply.warn('at start of done function, n is :' + n);
        if (n < requestCount) { //| final_status !=200) {
	        reply.warn('status of subrequest is :' + reply.status);
            if (reply.status == 200) {
	            reply.warn('lets set final_status to 200');
                final_status = 200;
                reply.warn('Value of final_status :' + final_status);
                reply.warn('!!! We return 200 because we have this one at
least, no matter if other are 404 !!!');
                res.return(200);
            }
        } else { // last response
            reply.warn('status of last subrequest is :' + reply.status);
            if (reply.status == 200) {
               reply.warn('lets set final_status for last subrequest to
200');
                final_status = 200;
                reply.warn('Value of final_status after last subrequest:' +
final_status);
                reply.warn('!!! We return the final 200 !!!');
                res.return(200);
            } else { // we did not get any 200
                reply.warn('!!! We return the final 403 !!!');
                res.return(403);
            }
        }
    };

    req.warn('n is :' + n);
    req.warn('final_status is : ' + final_status);
    req.warn('servicesCodes is : ' + servicesCodes);
    req.warn('requestCount is : ' + requestCount);

    for (var i = 0; i < requestCount; i++) {
        req.warn('Final status before sending subrequest to next service is
'+ final_status)
        if (final_status == 200) {
            req.warn('We stop here because we have the 200 !!!');
            break;
        } else {
            req.warn('subrequest is on : ' + servicesCodes[i]);
            req.subrequest("/" + servicesCodes[i] + "/TestDevice1.html", '',
done);
        }
    }
}


OUTPUT :


2018/10/30 16:00:10 [warn] 9220#9220: *15 js: Variables init ...
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: n is :0
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: final_status is : 403
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: servicesCodes is : rest,oass
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: requestCount is : 2
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: Final status before sending
subrequest to next service is 403
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: subrequest is on : rest
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: Final status before sending
subrequest to next service is 403
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: subrequest is on : oass
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: at start of done function, n
is :1
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: status of subrequest is :200
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: lets set final_status to 200
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: Value of final_status :200
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: !!! We return 200 because we
have this one at least, no matter if other are 404 !!!
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: at start of done function, n
is :2
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: status of last subrequest is
:404
2018/10/30 16:00:10 [warn] 9220#9220: *15 js: !!! We return the final 403
!!!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,281699,281746#msg-281746



More information about the nginx mailing list