0

i am a newbie trying to learn parse.com api. i can query my database and i can do stuff with the results of the query in the success handler. however i can not get the data into a variable outside of this. i think this is because it is making a call to the server and if i try and assign it to the a variable outside the query function, the query code has not executed before anything i try to do with the variable outside the query function.

MORE SPECIFICALLY: how do i get the alert(bitOfData); to execute after myObj has been assigned to bitOfData.

function getData(){
        var bitOfData = "";
        var DB = Parse.Object.extend("DB");
        var query = new Parse.Query(DB);
        query.equalTo("name", "myDatabase");
        query.find({
          success: function(results){
                   object = results[0];       
                   //alert(object.id);
                   var myObj = object.id;
                  alert(myObj);
                                  bitOfData = myObj;
                   },
          error: function(error){
                 alert(error.code);}
          });

    alert (bitOfData);   




}

getData();

1 Answer 1

1

The issue is that the code is async, the call to find(..) simply starts it running.

A great example I heard recently is that of boiling eggs. Your call to find(..) is like putting an egg on to boil and setting an alarm for when it is done (the success function). Once it is on the boil you go about doing other things in the kitchen.

If you want other things to happen when it finishes then you need to add that code to the success handler.

A more advanced topic is the use of "promises", there was a blog post on the parse.com blog about it that explains it.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.