The previous question was marked as a duplicate, with the link leading me here: How do I return the response from an asynchronous call?.
I still couldn't find a solution, but I guess i will need AJAX to return this object, because my functions/GET Request hasn't completed, which leads me to a new question. How can I adapt this to return asynchronously. I understand the concepet of Promises and async/await, but I'm unsure how I can implement this here so I can access the object globally.
[Original Question]
Returning object from the below function, but I'm receiving this error ReferenceError: object is not defined
. I would like to access this globally, but can't seem to access the object because of scope. Is there something that I'm missing?
When setting a global variable, this doesn't seem to update.
For example setting var globalObject = {};
outside and using globalObject = object
inside the object doesn't seem to change the variable {}
function getTicket (ticketID) {
var urlID = contentID;
var request = require("request");
var options = {
method: 'GET',
url: `https://www.mywebsite.com/api/${urlID}/body.json`,
headers: {'content-type': 'application/json', authorization: 'Basic PASSWORD=='}
};
request(options, function (response, body) {
var obj = JSON.parse(JSON.stringify(body));
var objContent = JSON.parse(obj.body);
var object = {
id: urlID,
url: 'https://www.mywebsite.com/api/' + urlID,
value: objContent
};
console.log(object.id);
console.log(object.url);
console.log(objContent.body[0].body);
});
return object;
}
getTicket(380289);