【问题标题】:how to update users for Meteor in the console如何在控制台中为 Meteor 更新用户
【发布时间】:2015-05-15 03:16:59
【问题描述】:

如何在 chrome 的控制台中更新 Meteor 用户? 这不会删除安装中包含的任何不安全和自动发布。

这是我在 chrome 控制台中输入的内容:

Meteor.users.update({_id: 'ARfiiEDnk6NbCEzcX'}, {$set: {displayName: 'test'}})

这是我得到的:

1

debug.js:41 更新失败:访问被拒绝

【问题讨论】:

标签: meteor meteor-accounts


【解决方案1】:

您无法更新Meteor.users 集合,因为出于安全原因,无论insecure 包是否存在,默认情况下都会禁用对该集合的写入。但是,此规则有两个例外:

  1. 客户可以创建一个新的用户帐户。
  2. 登录的用户可以更新其个人资料,例如{ $set: { 'profile.username': '' } }

如果您真的想更新用户文档上的不同字段,那么您至少有两种可能的选择:

  1. 在您的服务器上创建自定义方法 - 始终允许在服务器上写入
  2. 添加额外的Meteor.users.allow 规则,look here

我会推荐第一个解决方案,因为它更安全。

还有一件事。默认情况下,服务器只有publishes来自Meteor.users集合的几个字段,包括emailsprofile。如果您打算拥有更多它们并且希望它们在浏览器中可用,则需要添加自定义发布功能,例如

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

然后在客户端订阅:

Meteor.subscribe('myCustomField');

【讨论】:

  • 由于您要向用户添加displayName 密钥,因此只需将其放入配置文件中。 profile = Meteor.user().profile; profile.displayName = 'test'; Meteor.users.update({_id: 'ARfiiEDnk6NbCEzcX'}, {$set: {profile: profile}})
猜你喜欢
  • 2020-04-16
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多