I've searched for hours trying to fix this jQuery/JS code, But It just doesn't seem to want to return anything.
var result = getURLS(); // this is always blank
function getURLS() {
var urls = [];
var URL_record = Parse.Object.extend("URL_record");
var query = new Parse.Query(URL_record);
query.equalTo("user", Parse.User.current());
query.ascending("date");
query.find({
success : function(results) {
var tempURLS = [];
$.each(results, function(index, record) {
urls.push(record.get("shortURL") + " " + record.get("longURL"));
});
},
error : function(error) {
}
});
return urls;
}
Although if I create an alert function from this particular function:
success : function(results) {
var tempURLS = [];
$.each(results, function(index, record) {
urls.push(record.get("shortURL") + " " + record.get("longURL"));
});
alert(urls);
},
It seems to alert fine.
Any ideas?
query.findis (yet another) async code?tempURLSdo? It isn't used anywhere.Parsebut as @raina77ow said, I'm assumingquery.findis an asynchronous request. Basically that means the request is sent and then the code continues without waiting, which means it goes and returnsurls, before the code inside ofsuccessis executed.urlsis empty beforesuccessis executed.successhas executed?