0
在Meteor中,当前用户只有一个“名称”字段。我想在profile
下添加first_name
和last_name
。如何编辑和添加新字段给Meteor当前用户?
我user_edit.html
形式:
<template name="userEdit">
<div id="login-wrapper">
<form id="login-form" action="action" class="form login-form">
<div class="control-group">
<div class="controls">
<input name="first_name" type="text" value="" placeholder="First Name"/>
</div>
</div>
<div class="control-group">
<div class="controls">
<input name="last_name" type="text" value="" placeholder="Last Name"/>
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="submit" value="Submit" id="submit-website-button" class="btn btn-primary"/>
</div>
</div>
</form>
</div>
</template>
我对编辑当前用户的一种形式:
我也不太清楚如何在user_edit.js
更新当前用户,并增加额外的字段为第一和姓。下面的代码不起作用。
Template.userEdit.events({
'submit form': function(e) {
e.preventDefault();
var currentUserId = this._id;
var userProperties = {
first_name: $(e.target).find('[name=first_name]').val(),
last_name: $(e.target).find('[name=last_name]').val()
}
User.update(currentUserId, {$set: userProperties}, function(error) {
if (error) {
// display the error to the user
alert(error.reason);
} else {
Router.go('userPage', {_id: currentUserId});
}
});
}
});
编辑
这里是我的JS文件的应用答案后工作版本:
Template.userEdit.events({
'submit form': function(e) {
e.preventDefault();
Meteor.users.update(Meteor.userId(),
{
$set: {
'profile.first_name': $(e.target).find('[name=first_name]').val(),
'profile.last_name': $(e.target).find('[name=last_name]').val(),
'profile.cell_phone': $(e.target).find('[name=cell_phone]').val(),
'profile.email': $(e.target).find('[name=email]').val(),
'profile.business_name': $(e.target).find('[name=business_name]').val(),
'profile.website': $(e.target).find('[name=website]').val()
}
},
function(error) {
if (error) {
// display the error to the user
alert(error.reason);
} else {
Router.go('userPage', {_id: Meteor.userId() });
}
}
);
}
});
太棒了,谢谢! – fuzzybabybunny