如何使用Meteor.js显示所有LOGGED IN用户的列表 [英] How can I display a list of all LOGGED IN users with Meteor.js
问题描述
我一直在努力获取Meteor聊天应用中登录用户的列表。
我尝试了很多不同的东西。我设法在用户配置文件对象上添加登录标志。
服务器端:
I have been trying for days to get a list of logged in users in a Meteor chat app. I tried many different things. I managed to add a login flag on the user profile object. Server side:
Accounts.onCreateUser(function(options, user) {
if(!options.profile){
options.profile = {}
}
options.profile.login = false;
if (options.profile)
user.profile = options.profile;
return user;
});
在浏览器控制台中我得到:
In the browser console I get this:
Meteor.user().profile
Object {login: false}
所以这似乎有效。
现在我要列出用户是否已登录:
客户端
Now I want to list if users are logged in: Client side
Deps.autorun(function(){
if(Meteor.userId()){
Meteor.user().profile.login=true;
}
});
我登录后检查登录时仍然为false。
After checking the login remains false when I log in.
这个模板html给我一个所有用户名列表,但不是登录标志
This template html gives me a list of all usernames but not the login flag
{{#each allUsers}}
<p>{{username}}</p><p>{{profile.login}}</p>
{{/each}
所以我的问题是:profile.login仍然是假的我无法显示profile.login但会显示用户名。
So my problems are : profile.login remains false and I cannot display profile.login but the usernames are displayed.
提前感谢您。问候Joris
Thank you in advance. Greetings Joris
推荐答案
要更改用户 profile.login
属性,您需要做 Meteor.users.update(..)
或调用一个服务器方法来做到这一点。只是更改用户对象的属性将不起作用。
To change the users profile.login
property you need to do Meteor.users.update(..)
or call a server method that does that. Just changing the user object's property will not work.
通常我建议不要将用户状态保存到mondodb数据库中,而是将其保存在内存中的Collection中。
Generally I would recommend to not persist the users state into the mondodb database but hold it in a Collection in memory.
最简单的方法是使用其中一个包:
The easiest might be to just use one of these packages:
- < a href =https://github.com/dburles/meteor-presence/\"rel =nofollow> https://github.com/dburles/meteor-presence/
- https://github.com/mizzao/meteor-user-status
- https://github.com/dburles/meteor-presence/
- https://github.com/mizzao/meteor-user-status
或研究他们的源代码,了解如何传播用户状态。
or study their source code to see how to propagate the user status.
这篇关于如何使用Meteor.js显示所有LOGGED IN用户的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!