Javascript 如何在Meteor中为当前用户编辑和添加新字段?
在Meteor中,当前用户只有一个“name”字段。我想在Javascript 如何在Meteor中为当前用户编辑和添加新字段?,javascript,meteor,Javascript,Meteor,在Meteor中,当前用户只有一个“name”字段。我想在profile下添加first\u name和last\u name 我的user\u edit.html表单: <template name="userEdit"> <div id="login-wrapper"> <form id="login-form" action="action" class="form login-form"> <div class="cont
profile
下添加first\u name
和last\u name
我的user\u 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>
编辑
下面是应用答案后我的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() });
}
}
);
}
});
有几件事。我没有使用Meteor的过去几次更新,因此可能添加了“用户”作为光标来更新当前用户,但根据我的经验,这是您更新当前用户的方式:
Meteor.users.update( Meteor.userId(), { $set: { 'profile.first_name': "example" } } );
还请注意,我必须在代码段中指定'profile.first_name',当用户仅允许在客户端编辑概要文件部分时,您将尝试在用户对象的底部设置first_name和last_name。最后,我不确定模板上下文中的“this”是什么,因此我将设置currentUserId=Meteor.userId(),这是一种获取当前用户id的方便方法
Meteor.users.update( Meteor.userId(), { $set: { 'profile.first_name': "example" } } );