如何在parse.com中更新用户对象而不创建新的记录对象? [英] How to update a user object in parse.com not create a new record object?

查看:55
本文介绍了如何在parse.com中更新用户对象而不创建新的记录对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Parse.com和JavaScript SDK.

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.

我已在此处阅读说明 https://www.parse.com/docs /js_guide#objects-updating 但我仍然不确定我缺少什么.我觉得这很简单吗?

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/>

推荐答案

实际上,这很容易解决.现在,下面将更新对象,唯一更改的部分是:

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

代替使用var profileSave = Parse.User.current();

我用过

var profileSave = Parse.User.current();

现在完全有道理,在这里也可以找到一些有用的背景知识 更新parse.com中的用户对象

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

这篇关于如何在parse.com中更新用户对象而不创建新的记录对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆