0

Using Parse.com and JavaScript SDK.

The below code saves the data as a new user object in parse. But I want it to actually update the current user that is logged in and not create a new user object.

I've read the instructions here https://www.parse.com/docs/js_guide#objects-updating But I'm still not sure what I'm missing. I have a bad feeling it might be something simple?

      <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="div_password" style="display:none;"></div>
                        <div id="profile_pic"></div>
                        <div id="profile_pic_url"></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>
                                <input type="password" class="div_password" id="password" value="" />
                            </li>
                            <li>
                                <input type="file" class="div_profile_pic_url" id="profile_pic_url" value="" />
                            </li>

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

                            </li>

                        </ul>
                    </div>



                    <!--Footer stuff-->

                    <div class="container">

                        <div class="footer-socials">
                            <a href="#" class="facebook-footer"></a>
                            <a href="#" class="goup-footer"></a>
                            <a href="#" class="twitter-footer"></a>
                        </div>
                        <p class="copyright uppercase center-text no-bottom">Copyright 2014
                            <br>All rights reserved</p>

                        </div>
                        <div style="height:350px"></div>
                    </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("xxx", "xxx");




        // 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 password = user.get("password");
            var gender = user.get("gender");
            var imgPaht = user.get("pic");
            var profile_pic_url = 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())

            document.getElementById("div_password").innerHTML = password;
            $(".div_password")
            .val($("#div_password").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 User = Parse.Object.extend("_User");

    var profileSave = new User();

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

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

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

    }
    });         

    }
<script/>

2 Answers 2

0

It looks like you need to call profileSave.save() again within the success function, but this time WITHOUT arguments.

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

5 Comments

So you mean profileSave.save(null, { success: function(profileSave) { profileSave.save(); }, error: function(profileSave, error) { // Fail } ? if so I still get the same issue..
What is your issue exactly? The database isn't getting updated? Or the server isn't receiving anything at all?
The database is not being updated. Instead a new record is being created each time the "save" button is pressed. It should just update the current users details, i.e if I change my name. I've expanded the code so you can see the full picture
Ok I believe this is because you are creating a new object yourself before calling save(). Somehow, you need to associate that user profile's unique object ID to it before saving it to the database, perhaps by using set or fetching it from the DB. You could have 100 users in the database with the same username, and all unique object IDs, unless you put some kind of key on the actual username. Also, just because you create an element with a 'display:none' style on it, that doesn't mean the user can't see it. They could simply view the page source to see that information
thanks, but I actually worked it out in quite a simple method. See my answer.
0

Actually this was very simple to resolve. The below now updates the object, the only part changed is:

Instead of using var profileSave = Parse.User.current();

I used

var profileSave = Parse.User.current();

Which makes perfect sense now, some useful background knowledge can also be found here Update user object in parse.com

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.