0

I'm using an alert() for testing, but I can't get my object to alert the right value of my variables after they have been changed inside of my for loop. I want to store the values in an object using a constructor but only the empty values I set at the beginning are being stored.

//declare the variables I'm going to use inside of my database query and for loop. 

$(function(){
    var uID =  JSON.parse(localStorage.getItem( 'uID' ));
    var hood = ' ';
    var gender = ' ';
    var birthday = ' ';
    var zip = ' ';
    Parse.initialize("ivHLAO7z9ml1bBglUN*******yCgKM2x","gNeGt04lU7xce*****BsIBSCVj");

$("#mainDiv").on('click', '.interested', function(){
    //on click, use "uID" variable to query the parse database to get birthday, gender, neighborhood, and zip
    var query = new Parse.Query(Parse.User);
    query.equalTo("uID",uID);
    query.find({
        success: function(results) {
            for(i = 0; i < results.length; i++){
                //this is where the variable values declared at the beginning are supposed to be changed to the results of the query 
                hood = results[i].get("neighborhood");
                gender = results[i].get("gender");
                birthday = results[i].get("birthday");
                zip = results[i].get("zipCode");
            }
        }//closes success
    })//closes find 

    //my object constructor 

    function interested(neighborhood,sex, bDay, zipCode) {
        this.hood = neighborhood;
        this.gender = sex;
        this.birthday = bDay;
        this.zip = zipCode; 
    }
    var intrstd = new interested(hood, gender, birthday,zip);

    alert(intrstd.hood); 
    alert(intrstd.gender); 
    alert(intrstd.birthday); 
    alert(intrstd.zip); 
});//closes on
1
  • Try adding a breakpoint inside the success function and see when it's hit vs when the alert is fired. Commented May 3, 2013 at 20:00

1 Answer 1

3

If you query is asynchronous, then object is constructed before those variables change. Move your alert into correct scope:

  //declare the variables I'm going to use inside of my database query and for loop. 
$(function () {
    var uID = JSON.parse(localStorage.getItem('uID'));
    var hood = ' ';
    var gender = ' ';
    var birthday = ' ';
    var zip = ' ';
    Parse.initialize("ivHLAO7z9ml1bBglUN*******yCgKM2x", "gNeGt04lU7xce*****BsIBSCVj");

    $("#mainDiv").on('click', '.interested', function () {

        //on click, use "uID" variable to query the parse database to get birthday, gender, neighborhood, and zip

        var query = new Parse.Query(Parse.User);
        query.equalTo("uID", uID);

        //my object constructor 
        function interested(neighborhood, sex, bDay, zipCode) {
            this.hood = neighborhood;
            this.gender = sex;
            this.birthday = bDay;
            this.zip = zipCode;
        }

        query.find({
            success: function (results) {

                for (i = 0; i < results.length; i++) {

                    //this is where the variable values declared at the beginning are supposed to be changed to the results of the query 

                    hood = results[i].get("neighborhood");
                    gender = results[i].get("gender");
                    birthday = results[i].get("birthday");
                    zip = results[i].get("zipCode");
                }

                var intrstd = new interested(hood, gender, birthday, zip);

                alert(intrstd.hood);
                alert(intrstd.gender);
                alert(intrstd.birthday);
                alert(intrstd.zip);

            } //closes success
        }) //closes find 

    }); //closes on

});
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.