【问题标题】:meteorjs meteor-user-status mizzao package: cannot get other usersmeteorjs meteor-user-status mizzao 包:无法获取其他用户
【发布时间】:2016-04-22 11:56:12
【问题描述】:

试图实现 mizzao/meteor-user-status 用户状态包,但只获取当前登录用户(我)的状态。其他显示未定义。

/server 文件夹中的代码:

Meteor.publish('userStatus', function() {
  return Meteor.users.find({
    "status.online": true
  });
});

/client 文件夹中的代码:

Meteor.subscribe('userStatus');

Template.member.helpers({
  isOnline: function(username) {
    console.log(username); //gets each username, not just logged me logged in
    var currentUser = Meteor.users.findOne({
      username: username
    });
    console.log(currentUser.status.online); //gets undefined for other users other than me
    return currentUser.status.online; //returns undefined for other users
  }
});

也许这需要以某种方式引用“userStatus”而不是 Meteor.users.findOne(...),但是由于没有创建全局对象,例如像 UsersOnline = new Mongdb.Collection - 我不知道如何实现这一点。

Chrome 调试工具报错:TypeError: Cannot read property 'status' of undefined,显然,它只获取当前登录的用户。

编辑:澄清一下:我想我从 Mongo 中的自定义“配置文件”集合中获取其他用户,该集合是为了克服由于安全原因而无法读取“用户”集合的问题。所以我得到了配置文件列表,并为这个应该使用 mizzao 包的函数提供用户名,以查看它们是否在线。由于包从“用户”集合中读取,它只返回当前用户(我)。

如果我可以修改包以将其写入我的“个人资料”集合,我会完全没问题,如果有人建议这样做的话。

另一个更新: 我在服务器上运行了计数,它只返回一个(我猜是我)。 var cursor = Meteor.users.find({ “status.online”:真 }, { 字段:{ _id:1, 状态:1, 用户名:1 // 您可能需要的任何其他字段 } }); console.log(cursor.count());

【问题讨论】:

    标签: meteor


    【解决方案1】:

    如果您有这样的出版物:

    Meteor.publish('userStatus', function() {
      return Meteor.users.find({
        "status.online": true
      });
    });
    

    它将返回仅在线的用户。所以你得到undefined 对于你以外的用户,因为他们没有登录。

    尝试同时以其他用户身份登录(即使用不同的浏览器),看看您现在是否可以看到该用户的状态。

    您可以随时将您的出版物更改为:

    Meteor.publish('userStatus', function() {
      return Meteor.users.find({}, {fields: {username : 1, status : 1}});
    });
    

    您将获得所有用户的状态。但是如果你只需要检查“在线”标志(即你不需要软件包的其他功能),最好只发送有关在线用户的数据,并将未发布的用户视为离线。

    【讨论】:

    • 太棒了。像魅力一样工作。
    • 只是好奇 - 这个答案与我下面的答案有何不同?
    【解决方案2】:

    我猜你想在订阅准备好之前阅读用户。

    服务器:

    Meteor.publish('userStatus', function() {
      return Meteor.users.find({
        "status.online": true
      }, {
        fields:{
           _id: 1,
           status: 1,
           profile: 1,
           username: 1
           // Any other fields you may need
        }
      });
    });
    

    客户端(您应该返回游标,一旦订阅准备好,它将重新运行):

    Meteor.subscribe('userStatus');
    
    Template.member.helpers({
      isOnline: function(username) {
        console.log(username); //gets each username, not just logged me logged in
        var cursor = Meteor.users.find({username: username});
        if(cursor.count()){
           var currentUser = cursor.fetch()[0];
           if(currentUser.status && currentUser.status.online){
             console.log(currentUser.status.online); //gets undefined for other users other than me
             return currentUser.status.online; //returns undefined for other users
           }
        }
        return {};
      }
    });
    

    【讨论】:

    • 对不起,仍然只将我作为用户而忽略其他人。如果有帮助,我已删除自动发布和不安全。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2016-02-04
    相关资源
    最近更新 更多