且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法使用'roles'包向meteor用户添加角色

更新时间:2023-11-24 18:06:10

如果查看角色包你将看到他们使用你传入的user / userId来对用户的集合执行查询( here ,从第〜623行开始:

If you look at the code being used in the Roles package you will see that they use your passed in user/userId to perform a query on the user's collection (here, starting at line ~623):

try {
  if (Meteor.isClient) {
    // On client, iterate over each user to fulfill Meteor's
    // 'one update per ID' policy
    _.each(users, function (user) {
      Meteor.users.update({_id: user}, update)
    })
  } else {
    // On the server we can use MongoDB's $in operator for
    // better performance
    Meteor.users.update(
      {_id: {$in: users}},
      update,
      {multi: true})
  }
}

由于 onCreateUser on( docs 将返回的文档直接插入Meteor.users集合),角色在执行查询时找不到它。

Since onCreateUser is called before the user object is inserted into the collection (docs: The returned document is inserted directly into the Meteor.users collection), Roles cannot find it when it performs the query.

为了解决此问题,您必须等到用户插入集合。如果您查看角色包,他们的所有示例都会显示此信息。像这里,(第二个例子,添加评论),以及许多其他人:

In order to fix this you must wait until the user is inserted into the collection. If you look at the Roles package all of their examples show this. Like here, (second example, comments added), along with many others:

// insert user and retrieve the id
id = Accounts.createUser({
  email: user.email,
  password: "apple1",
  profile: { name: user.name }
});

// now we can verify that the user was inserted and add permissions
if (user.roles.length > 0) {
  Roles.addUsersToRoles(id, user.roles);
}

希望能够解决您的问题。所以基本上只需插入用户,然后添加权限。

Hope that shines some light on your issue. So basically just insert the user and then add the permissions after.