1

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?

5
  • Let me guess: query.find is (yet another) async code? Commented Oct 23, 2012 at 22:11
  • What does tempURLS do? It isn't used anywhere. Commented Oct 23, 2012 at 22:12
  • query.find is an async code but I wouldn't get a straight answer from anywhere else. and tempURLS was used earlier for debugging, it's not meant to be there. Commented Oct 23, 2012 at 22:17
  • 1
    I'm not familiar with Parse but as @raina77ow said, I'm assuming query.find is an asynchronous request. Basically that means the request is sent and then the code continues without waiting, which means it goes and returns urls, before the code inside of success is executed. urls is empty before success is executed. Commented Oct 23, 2012 at 22:18
  • so is there some sort of function to make it wait until the code inside success has executed? Commented Oct 23, 2012 at 22:22

2 Answers 2

2

query.find is asynchronous, you'll need to set the variable inside of the success function then call the code that uses result.

var result;
getURLS();

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"));
            });
            result = urls;
            // call code that uses result here
            processResults(result);
        },
        error : function(error) {

        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

I figured out that query.get is asynchronous after being stumped for hours. Until I stripped everything else out of my $(function () {}); block and saw on the console that the query fires after the block. The eventual idea is to load models and views into a Facebook app, which also has an asynchronous init function. I did not expect the Parse query to also be asynchronous.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.