【问题标题】:Meteor - Method adding user custom field do not workMeteor - 添加用户自定义字段的方法不起作用
【发布时间】:2016-05-19 05:48:20
【问题描述】:

我正在尝试通过一种方法向 Meteor.Users 添加自定义“isContributor”字段,但不知何故它没有添加该字段。 (我什至在对谁可以进行此更新进行任何安全检查之前就在谈论)。

在客户端我有以下事件:

Template.Articles.events({
'click #BeContributor': function() {
    userId = Meteor.userId();
    Meteor.call('setContributorState', userId);
}

});

在 server/main.js 中如下:

Meteor.methods({
    setContributorState: function(userId) {
        Meteor.users.update(userId, {
          $set: {
                isContributor: true
                }
        });
    }
});

不知何故,它不会为我的用户添加该字段。没有控制台或服务器错误。我想我错过了添加该领域的权利。有什么想法吗?

提前致谢。

--- 编辑 ---

实际上该方法有效,但我没有发布结果,因此我无法在 MeteorToys 中检查该字段是否已更新。通过以下出版物,它可以工作:

Meteor.publish(null, function(){
    return Meteor.users.find({_id: this.userId}, {fields: {isContributor: 1}});
});

发现于Publishing custom Meteor.user() fields

【问题讨论】:

  • 在你的服务器方法中尝试console.log 来检查它是否真的被调用了。
  • 刚刚检查过:方法被调用并且 UserId 在那之前被正确执行。更新似乎不起作用。

标签: javascript meteor methods


【解决方案1】:

如果你要修改登录用户的贡献者状态,你不需要传递任何参数。这是一个安全漏洞。服务器有调用请求的信息。

Meteor.methods({
setContributorState: function() {
    Meteor.users.update({_id:this.userId}, {
      $set: {
            isContributor: true
            }
    });
}
});

【讨论】:

  • 感谢您的安全说明。
猜你喜欢
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-16
  • 1970-01-01
相关资源
最近更新 更多