【问题标题】:Add extra user field添加额外的用户字段
【发布时间】:2014-06-05 02:14:30
【问题描述】:

在我的 Meteor 应用程序中,我使用默认的 accounts 包,它为我提供了默认的登录和注册功能。现在我想给用户添加一个额外的字段,比如nickname,并且让登录的用户可以编辑这些信息。

为了编辑个人资料,我想我应该这样做:

Template.profileEdit.events({
  'submit form': function(e) {
    e.preventDefault();

    if(!Meteor.user())
      throw new Meteor.Error(401, "You need to login first");

    var currentUserId = this._id;

    var user = {
      "profile.nickname": $(e.target).find('[name=nickname]').val()
    };

    Meteor.users.update(currentUserId, {
      $set: user
    }, function(error){
      if(error){
        alert(error.reason);
      } else {
        Router.go('myProfile', {_id: currentUserId});
      }
    });

  }
});

但是如果我在 Mongo 中查看,我不会存储信息。同样在显示配置文件时,{{profile.nickname}} 返回空。这里有什么问题?

编辑:添加collections\users.js 以显示权限:

Meteor.users.allow({
  update: function (userId, doc) {
    if (userId && doc._id === userId) {
      return true;
    }
  }
});

Meteor.users.deny({
  update: function(userId, user, fieldNames) {
    return (_.without(fieldNames, 'profile.nickname').length > 0);
  }
});

【问题讨论】:

    标签: meteor


    【解决方案1】:

    是的,我认为应该可以完成这项工作,尽管我还没有实际运行代码。这个想法当然是对的。

    需要注意的主要事项是:

    1. 必须允许从客户端编辑用户文档,并在服务器上使用适当的 Meteor.users.allow() 块,假设您要删除“不安全”包(在生产中执行任何操作之前需要这样做) .
    2. 事实上,"by default the server publishes username, emails, and profile",所以你需要在服务器上编写一个Meteor.publish 函数并订阅它,如果你想在删除用户文档中向客户端公开任何其他字段“autopublish” 包(同样,你真的应该这样做)。

    【讨论】:

    • 这行得通,但是当我查看数据库时,我没有看到 full_name 存储的属性?
    • 你是用meteor mongo shell 看的吗?
    • 是的,我已经将上面的profile_edit.js 更新为现在的状态。显示个人资料时,{{profile.name}} 为空。
    • 会不会是权限问题?
    • 你有autopublish,如果没有,你有发布功能吗?实际上,您不需要在profile 中看到某些内容@
    猜你喜欢
    • 1970-01-01
    • 2013-04-01
    • 2020-05-17
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2021-08-25
    相关资源
    最近更新 更多