Wednesday, 17 July 2019

How do I make a nested function variable globally available?

Hi, I'm new to node.js so this is probably obvious to some you vets.I'm trying to make the variable "test" under function(chunk) available globally..but nothing I do is working. It logs as undefined.I've tried..without var in-front of the variable.defining the variable outside of the function (I'm not sure how to do that with nested functions..or if I did it right)setting the variable as GLOBAL.test = inside the functionWhat is the proper way to do this?process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; var test var req = https.request(optionspost, function(res) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); var test res.on('data', function (chunk) { console.log('BODY: ' + chunk); console.log(chunk) obj = JSON.parse(chunk) var keyArray = Object.keys(obj); // key1 test = obj.result.data console.log(obj.result.data) }); }); console.log('test') console.log(testy) //logs as undefined console.log('test')

Submitted July 18, 2019 at 12:46AM by Bar_of_the_Dock

No comments:

Post a Comment