【问题标题】:How to update a user object in parse.com not create a new record object?如何更新 parse.com 中的用户对象而不是创建新的记录对象?
【发布时间】:2014-10-29 04:41:20
【问题描述】:

使用 Parse.com 和 JavaScript SDK。

以下代码将数据保存为解析中的新用户对象。但我希望它真正更新当前登录的用户,而不是创建新的用户对象。

我已阅读此处的说明https://www.parse.com/docs/js_guide#objects-updating 但我仍然不确定我缺少什么。我有一种不好的预感,它可能很简单?

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

【问题讨论】:

    标签: javascript jquery parse-platform


    【解决方案1】:

    看起来您需要在成功函数中再次调用profileSave.save(),但这次没有参数。

    【讨论】:

    • 所以你的意思是 profileSave.save(null, { success: function(profileSave) { profileSave.save(); }, error: function(profileSave, error) { // Fail } ? 如果是我仍然遇到同样的问题..
    • 您的问题到底是什么?数据库没有更新?或者服务器根本没有收到任何东西?
    • 数据库未更新。相反,每次按下“保存”按钮时都会创建一条新记录。它应该只更新当前用户的详细信息,即如果我更改了我的名字。我已经扩展了代码,所以你可以看到完整的图片
    • 好的,我相信这是因为您在调用 save() 之前自己创建了一个新对象。不知何故,您需要将该用户配置文件的唯一对象 ID 与其相关联,然后再将其保存到数据库中,可能是通过使用 set 或从数据库中获取它。您可以在数据库中有 100 个用户具有相同的用户名和所有唯一的对象 ID,除非您在实际用户名上放置某种键。此外,仅仅因为您创建了一个带有 'display:none' 样式的元素,这并不意味着用户看不到它。他们可以简单地查看页面源以查看该信息
    • 谢谢,但我实际上用一种非常简单的方法解决了这个问题。看我的回答。
    【解决方案2】:

    其实这很容易解决。下面现在更新了对象,唯一改变的部分是:

    而不是使用 var profileSave = Parse.User.current();

    我用过

    var profileSave = Parse.User.current();
    

    现在说得很有道理,一些有用的背景知识也可以在这里找到 Update user object in parse.com

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2017-02-25
      • 1970-01-01
      • 2018-10-25
      • 2015-07-26
      • 2019-03-12
      • 2017-04-12
      相关资源
      最近更新 更多