0

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>
6
  • @Code Uniquely Do you want to give the example in the answer and I can accept it?@ Commented Oct 25, 2014 at 6:42
  • @CodeUniquely Are you sure that's the reason? parse.com/docs/js_guide#objects-classes Commented Oct 25, 2014 at 6:43
  • There's no need to call .toString(). .val() always returns strings. Commented Oct 25, 2014 at 6:53
  • @Barmar ok, even with that edit the issue seems to still exist.. Commented Oct 25, 2014 at 6:57
  • I know, it has nothing to do with the error you're getting. It was just a comment, not an answer. Commented Oct 25, 2014 at 6:58

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks, when saving back, must I include the password? it doesn't like this is optional?
Sorry, but I have no idea. Your code doesn't seem to mention any password, I don't know what properties exist on any of your objects or what your application actually expects. If your application needs you to send one then you must, if you don't need to send one then you may choose to or not, its up to you really...

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.