I recently wrote a function that retrieves JSON data from a specified URL. Here's what the function looks like:
function getJSON(url) {
request.get({
url: url,
json: true,
headers: { 'User-Agent': 'request' }
}, (err, res, response) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
// Successfully received JSON data
return response;
}
});
}
Although the function is functioning properly, I'm facing an issue where the variable I declare to store the function's returned value ends up being undefined
instead of an object as expected.
var someVar = someFunction('url-to-the-json');