2014-05-12 44 views
0

在Meteor中,当前用户只有一个“名称”字段。我想在profile下添加first_namelast_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() }); 
     } 
     } 
    ); 
    } 
}); 

回答

1

几件事情。我没有使用过去的几个,所以也许“用户”已作为一个光标流星的更新来更新当前用户,但根据我的经验,这是你将如何更新当前用户:

Meteor.users.update(Meteor.userId(), { $set: { 'profile.first_name': "example" } }); 

而且请注意,我必须指定'profile.first_name',在您的代码片段中,当用户只能编辑配置文件部分客户端时,您将尝试在用户对象的基础中设置first_name和last_name。最后,我不确定在模板的上下文中有什么'this',所以我会设置currentUserId = Meteor.userId(),这是获取当前用户ID的一种便捷方式。

+0

太棒了,谢谢! – fuzzybabybunny

相关问题