meteorjs meteor-user-status mizzao package:无法获得其他用户

时间:2016-01-17 06:06:12

标签: meteor

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

/ 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:无法读取未定义的属性“status”,显然,它只能获取当前登录的用户。

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

如果我可以修改软件包以便写入我的“个人资料”集合,如果有人建议如何做,我会完全没问题。

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

2 个答案:

答案 0 :(得分: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}});
});

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

答案 1 :(得分:0)

我猜您在订阅准备就绪之前尝试读取用户。

服务器:

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 {};
  }
});