How can I display a list of all LOGGED IN users with Meteor.js

徘徊边缘 提交于 2019-12-02 06:02:25

问题


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}

So that seems to work.

Now I want to list if users are logged in: Client side

Deps.autorun(function(){
    if(Meteor.userId()){
        Meteor.user().profile.login=true;
    }
});

After checking the login remains false when I log in.

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}

So my problems are : profile.login remains false and I cannot display profile.login but the usernames are displayed.

Thank you in advance. Greetings Joris


回答1:


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.

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:

  • 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.



来源:https://stackoverflow.com/questions/25643114/how-can-i-display-a-list-of-all-logged-in-users-with-meteor-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!