After researching online, I came across a similar question marked as a duplicate that brought me to this link: How do I return the response from an asynchronous call?.
Even though I couldn't find a solution in that thread, it seems like I may need to resort to AJAX for returning this object. Since my functions and GET Request haven't completed, I have a new query on how to adapt this for asynchronous return. While I grasp the concept of Promises and async/await, I'm still uncertain about implementing them in order to access the object globally.
[Original Question]
I am currently trying to return an object within the below function but keep encountering the error message
ReferenceError: object is not defined
. My objective is to be able to access this object globally, however, the scope seems to prevent me from doing so. Am I missing something crucial here?
Whenever I attempt to set a global variable, it doesn't get updated accordingly.
For instance, when I define var globalObject = {};
outside and then assign globalObject = object
inside the object, it fails to modify 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);