Welcome! Log In Create A New Profile

Advanced

Re: Variable scope in javascript module

October 30, 2018 12:07PM
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 !!!
Subject Author Posted

Variable scope in javascript module

alweiss October 26, 2018 02:16AM

Re: Variable scope in javascript module

Dmitry Volyntsev October 26, 2018 05:04AM

Re: Variable scope in javascript module

alweiss October 30, 2018 12:07PM

Re: Variable scope in javascript module

alweiss October 30, 2018 12:58PM

Re: Variable scope in javascript module

Dmitry Volyntsev October 30, 2018 01:10PM

Re: Variable scope in javascript module

Valentin V. Bartenev October 30, 2018 01:12PM

Re: Variable scope in javascript module

alweiss October 30, 2018 05:55PM

Re: Variable scope in javascript module

Valentin V. Bartenev October 30, 2018 06:28PM

Re: Variable scope in javascript module

Maxim Konovalov October 30, 2018 06:32PM

Re: Variable scope in javascript module

alweiss October 30, 2018 07:22PM

Re: Variable scope in javascript module

alweiss October 30, 2018 06:47PM

Re: Variable scope in javascript module

Valentin V. Bartenev October 30, 2018 07:58PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 65
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready