Dano007
Dano007

Reputation: 1932

`Uncaught TypeError: undefined is not a function` error trying to save data

Using Parse and JavaScript SDK.

I'm unable to see what is wrong with this function that should save the edited data on the page back into parse. I'm getting a Uncaught TypeError: undefined is not a function where I'm trying to do

profileSave.set("username", saveusername);
profileSave.set("email", saveemail.toString);
profileSave.set("gender", savegender.toString);

But do not understand why?

// Saves the users profile information 
$('#save').click(function (e) {
    ProfileSave();
}); 
///

function ProfileSave() {

    var profileSave = Parse.Object.extend("_User");


    var saveusername = $('#username').val();
    var saveemail = $('#email').val();
    var savegender = $('#gender').val();

    profileSave.set("username", saveusername.toString);
    profileSave.set("email", saveemail.toString);
    profileSave.set("gender", savegender.toString);

    profileSave.save(null, {
        success: function(profileSave) {
//success
},
error: function(profileSave, error) {
 // Fail

}
});         

}

Complete code

                <div class="container">
                    <!--Data is stored in these divs but not they are not displayed to the user!-->

                    <div id="div_uname" style="display:none;"></div>
                    <div id="div_email" style="display:none;"></div>
                    <div id="div_gender" style="display:none;"></div>
                    <div id="profile_pic"></div>

                </div>  

                <!--This is what the user sees. The id calls JS that enables the input field to be edited!-->

                <!--Displays user profile information on the page!-->

                <div class="container">

                    <h4>General Features</h4>
                    <ul>

                        <li>
                            <input type="text" class="div_uname" id="username" value="" />
                        </li>
                        <li>
                            <input type="text" class="div_email" id="email" value="" />
                        </li>
                        <li>
                            <input type="text" class="div_gender" id="gender" value="" />
                        </li>

                        <li>
                            <button class="button button-blue" id="save">Save Name</button>

                        </li>

                    </ul>
                </div>






                <!--This script displays the user profile data on the page and allows the user to update the details -->
                <script type="text/javascript">
                    Parse.initialize("xx", "xx");


                         Parse.User.logIn("dave", "delvedia", {
  success: function(user) {
    console.log("Logged in!");
  }
});

    // Makes sure the current user is selected//

    // Pulls the user data from parse and stores in variables//
    var user = Parse.User.current();
    user.fetch().then(function(fetchedUser) {
        var username = fetchedUser.getUsername();
        var email = fetchedUser.getEmail();
        var gender = user.get("gender");
        var imgPaht = user.get("pic");


        // Outputs the data to the users view//

        // Adds the contents of the variables into the html divs//

        document.getElementById("div_uname").innerHTML = username;
        $(".div_uname")
        .val($("#div_uname").text())

        document.getElementById("div_email").innerHTML = email;
        $(".div_email")
        .val($("#div_email").text())

        document.getElementById("div_gender").innerHTML = gender;
        $(".div_gender")
        .val($("#div_gender").text())


        $('<img src="' + imgPaht + '">').load(function() {
            $(this).width(400).height(400).appendTo('#profile_pic');
        })


    }, function(error) {

    });




// Saves the users profile information 
$('#save').click(function (e) {
    ProfileSave();
}); 
///

function ProfileSave() {

    var profileSave = Parse.Object.extend("_User");


    var saveusername = $('#username').val();
    var saveemail = $('#email').val();
    var savegender = $('#gender').val();

    profileSave.set("username", saveusername.toString());
    profileSave.set("email", saveemail.toString());
    profileSave.set("gender", savegender.toString());

    profileSave.save(null, {
        success: function(profileSave) {
//success
},
error: function(profileSave, error) {
 // Fail

}
});         

}
</script>

Upvotes: 0

Views: 1274

Answers (1)

Code Uniquely
Code Uniquely

Reputation: 6373

You need to create an object on which to call the set. So you define a variable that extends the object you are interested in.

var User = Parse.Object.extend("_User");

and then you can create new Object of that extended type

var profileSave = new User();

and the you can call your set(s) and get(s)

profileSave.set("username", saveusername);
profileSave.set("email", saveemail);
profileSave.set("gender", savegender);

Upvotes: 1

Related Questions