Meteor - 从用户列表中删除当前用户

4

我目前正在发布一个组中用户列表。虽然这个功能是可行的,但我无法阻止当前用户出现在列表中。如何删除当前用户?感谢任何建议和帮助。

路径:publish.js

Meteor.publish('studentList', function (group) {
  if (Roles.userIsInRole(this.userId, ['teacher'], group)) {

  return Meteor.users.find({roles:'student'}, {fields:{emails:1, profile: 1, roles: 1}});

  } else {

    // user not authorized. do not publish secrets
    this.stop();
    return;

  }
});

路径:studentList.js
Template.studentList.onCreated(function() {
    var self = this;
    self.autorun(function() {
        self.subscribe('studentList')
    });
});

Template.studentList.helpers({
    studentList: ()=> { 
        return Meteor.users.find({}); 
    },
});
3个回答

3
在客户端代码中,您可以排除与当前用户匹配的用户ID的用户:
Template.studentList.helpers({
    studentList: ()=> { 
        return Meteor.users.find({_id: { $ne: Meteor.userId() }}); 
    },
});

或者你可以通过执行Meteor.users.find({}).fetch().someRemoveLogic()在客户端从结果中移除它。


在发布中这样做没有帮助,因为用户对象是自动发布的。在辅助函数中排除它才是正确的方法。 - Michel Floyd
谢谢。我以为这与Meteor.user()自动发布当前用户的个人资料有关。我只是不知道如何在helpers中删除它。再次感谢大家。 - bp123
1
这种方法的问题在于它总是会移除当前用户,如果我们只想在它不匹配实际过滤器和限制时才移除当前用户怎么办? - Archimedes Trajano
然后你可以添加一个检查来引用过滤器,并将其用作find()命令的输入。 - Brett McLain

3

默认情况下,已登录用户的数据已订阅到客户端。没有办法取消这个订阅。因为在幕后,Meteor.user()从这个订阅中工作。如果您需要从客户端辅助程序中排除它,可以创建一个查询来排除当前用户的数据。例如:

Meteor.users.find({_id: { $ne: Meteor.userId()}}); 

0
最近在我们的Meteor应用程序中终于移除了aldeed:tabular插件时遇到了这个问题。这里有一个优雅的服务器端解决方案。
对于发布Meteor.users列表(可能是分页的),可以使用cursor observe function为每个已发布的文档添加一个特殊标志forUserList。
Meteor.publish('userTable', function(skip, limit) {
   //  ...checks and validation code
   
   const self = this;
   const usersHandle = Meteor.users.find({}, {
      fields: {
         profile: 1,
         ...
      },
      skip: skip,
      limit: limit
   }).observe({
      added: function(addedUser) {
        //  Add a flag so we know the users come from this publication
        addedUser.forUserList = true;
        self.added("users", addedUser._id, addedUser);
      },
      changed: function(changedUser) {
        self.changed("users", changedUser._id, changedUser);
      },
      removed: function(removedUser) {
        self.removed("users", removedUser._id);
      }
   });

   //  Mark the subscription as ready
   self.ready();

   // Stop observing the cursor when client unsubs.
   self.onStop(function () {
      usersHandle.stop();
   });
}   

然后在客户端上,您可以向Mini-Mongo传递一个查询,以获取您添加的标记。
//  Cursor function
instance.users = function() {
   //  Set up the client query to make sure we only include documents from this datatable (as this can return
    //  ALL documents on the client). So this is best practice.
    const query = {
      forUserList = true;
    };
    const sort = {
      ...
    };
    return instance.data.collection.find(query, {sort: sort});
};

这将只在上述发布实际找到当前登录用户时显示。因此,如果您正在进行用户的分页列表,当前登录用户会像正常情况下出现在分页列表中,并获得forUserList标志,从而在列表中显示。但是当它们被分页处理时,登录的用户对象将缺少该标志,因此不会出现在模板中,因为 mini-mongo 查询。

使用游标观察函数非常灵活和强大,可用于操作发布的文档。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接