当用户尝试更新自己的个人资料时,流星"update failed: Access denied"?



im将字段添加到用户的创建帐户中。这很好:

Accounts.onCreateUser((options, user) => {
  user.groups = [2];
  return user;
});

我需要制作一个允许用户更改此功能的函数。当我从前端运行此操作时,我会遇到一个错误"更新失败:访问被拒绝"

Meteor.users.update(
  { _id: Meteor.userId() },
  {
    $set: { groups: [4, 5] },
  },
);

在服务器/main.js中我有:

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

不要直接从客户端进行数据库更新。不能信任客户。

话虽如此,有两种方法可以解决:

一个

根据文档:

默认情况下,当前用户的用户名,电子邮件和个人资料是 发表给客户。您可以发布其他字段 当前用户:

// Server
Meteor.publish('userData', function () {
  if (this.userId) {
    return Meteor.users.find({ _id: this.userId }, {
      fields: { groups: 1 }
    });
  } else {
    this.ready();
  }
});

Meteor.users.allow({{ 更新:功能(用户,用户){ 返回true;

/**
 * Don't use `return true` in production!
 * You probably need something like this:
 * return Meteor.users.findOne(userId).profile.isAdmin;
 */

}});

// Client
Meteor.subscribe('userData');

流星允许规则

两个

定义服务器中的流星方法,并在执行一些验证后,让服务器为您更新相关数据。服务器代码始终被信任。

server.js

Meteor.method({
updateGroups: function(data){
 // make changes to the user record
});

相关内容

最新更新