I'm trying to retrieve an entire object from a Parse database, but this is not a Parse-specific problem. I'm trying to use the object's unique ID to retrieve the whole object and assign it to the resultObject variable, so that I can save a reference to the resultObject later within this same addPerk function.
What's happening is that the alert() on the final line of the block below si coming back saying that resultObject is undefined. I believe this is because the objectQuery's success function is called after the alert() line, due to the asynchronous nature. However, I thought I could fix this by making a while() loop to make the program wait until resultObject is defined. Why doesn't this work? How else could I get the resultObject out of the scope of the success function?
I don't want to move all of my remaining logic into the success block of the query, I'm already experiencing major "pyramid of death" and don't want to make the code any more difficult to read.
addPerk: function()
{
var resultObject;
var Object = Parse.Object.extend("Object");
objectQuery = new Parse.Query(Object);
objectQuery.get(objectId, {
success: function(object) {
alert("returned object in success function is " + object);
resultObject = object;
alert("resultObject in success function is:" + resultObject);
},
error: function(object, error) {
alert("error: " + error.code + ", " + error.message);
}
});
while (typeof correctProvider == "undefined") {
setTimeout(null, 3000);
}
alert("resultObject (after WHILE loop) is: " + resultObject);