I've created a function that should return a parsed JSON file, but I'm having trouble passing values to a variable.
var getData = function(){
var parsedData = {};
var req = http.request(options, function (res){
var resBody = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
resBody += chunk;
});
res.on('end', function(){
parsedData = JSON.parse(resBody);
})
});
req.end();
console.log(parsedData.responseHeader); /* quick test to prove itβs working */
return parsedData;
}
module.exports = getData;
parsedData
was declared outside of the scope of the http.request
function, but I can't set a value to it. I can print the value when console.log(parsedData.responseHeader)
is in the scope of http.request
, but I can't get the value outside of that scope.