2014-10-28 131 views
0

使用Parse.com和JavaScript SDK。如何更新parse.com中的用户对象而不是创建新的记录对象?

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

我已阅读说明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/> 

回答

0

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

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

我用

var profileSave = Parse.User.current(); 

这是非常合情合理的,现在,一些有用的背景知识,也可以在这里找到 Update user object in parse.com

0

它看起来像你需要成功函数中再次调用profileSave.save(),但这次没有争论。

+0

所以你的意思\t profileSave.save(空,{ \t \t成功:函数(profileSav e){ profileSave.save(); }, 错误:函数(profileSave,error){ //失败 }?如果是的话,我仍然会得到同样的问题。 – Dano007 2014-10-28 17:31:39

+0

究竟是什么问题?数据库没有得到更新?或者服务器根本没有收到任何东西? – 2014-10-28 17:38:58

+0

数据库未更新。相反,每次按下“保存”按钮时都会创建新记录。它应该只更新当前用户的详细信息,即如果我更改了我的名字。我已经扩展了代码,所以你可以看到完整的图片 – Dano007 2014-10-28 17:41:31

相关问题