【发布时间】: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