【问题标题】:is there any meteor package to implement user status for custom user collection?是否有任何流星包来实现自定义用户集合的用户状态?
【发布时间】:2026-01-05 20:05:02
【问题描述】:

是否有任何流星包来实现自定义用户集合的用户状态而不是默认的。我实现了“myUser”集合而不是默认的“meteor.user”集合,我想向它添加状态字段?

【问题讨论】:

  • 有什么解决方案可以在服务器端访问我的自定义当前 userId 吗?

标签: meteor


【解决方案1】:

如果您有自定义集合,只需在服务器端执行此操作:

export const myUser = new Mongo.Collection('myUser');

const userData = (userId) => {
  return myUser.find(userId);
}

您现在将拥有一个对象,其中所有用户字段都可在服务器端访问。要添加状态字段,在客户端只需对用户执行的每个功能执行 Meteor.call。 (点击事件,提交到数据库等)

Meteor.methods({
  'userOnline'(userId) {
     myUser.update({_id:userId},{$set:{onlineDate:new Date()}})
  }
}

Meteor.call('userOnline', userId);

然后查询您的数据库您想要的超时时间。最后 n 秒、分钟、小时。

query = {
    onlineDate: { // 18 minutes ago (from now)
        $gt: new Date(ISODate().getTime() - 1000 * 60 * 18)
    }
}

【讨论】:

    【解决方案2】:

    您可以在服务器端的用户对象的根目录中添加任何字段,只需确保您订阅了像这样的客户端发布

    /* eslint-disable prefer-arrow-callback */
    import { Meteor } from 'meteor/meteor';
    
    Meteor.publish('userExtraFields', function userExtraFields() {
      if (this.userId) {
        return Meteor.users.find(
          { _id: this.userId },
          { fields: { currentTeamId: 1, teams: 1, isAdmin: 1, isCreator: 1 } }
        );
      }
      return false;
    });
    

    【讨论】:

    • 谢谢。但我不想将自定义字段添加到meteor.users。我在外部MongoDb上有我的用户集合,我想检测女巫用户在线并存储其状态
    • 对不起^^也许你正在寻找这个atmospherejs.com/konecty/user-presence
    • 不幸的是,这些包与meteor.user集合一起使用
    • 这是一个普通的 Meteor 模式,将用户收集在外部 MongoDB 中,您可以将所有数据存储在那里