使用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/>
答案 0 :(得分:0)
看起来您需要在成功函数中再次调用profileSave.save()
,但这次没有参数。
答案 1 :(得分:0)
实际上这很容易解决。下面现在更新对象,唯一改变的部分是:
而不是使用var profileSave = Parse.User.current();
我用过
var profileSave = Parse.User.current();
现在很有意义,这里也可以找到一些有用的背景知识 Update user object in parse.com