I am new to Javascript, so pardon me my trivial mistakes. I have written the following script in which res
is supposed to be a Global variable in the script file. I am setting the global res
in a method and then accessing it in another method, however the issue is, this.issue
returns undefined
in method Y but it isn't undefined in the method X where I assign a value to it. Why is that?
Method X is called before Method Y.
var res;
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express', shortenedUrl: ''});
});
router.X('/yolo', jsonParser, function(req, res, next) {
this.res = res;
console.log("this.res: "+ this.res); // this.res not undefined here
Y();
});
function Y(){
console.log("this.res: "+ this.res); // this.res here becomes undefined
});