3
votes

Trying to implement mizzao/meteor-user-status user status package, but only getting the status for the currently logged in user (me). Others show undefined.

Code in /server folder:

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

Code in /client folder:

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

Maybe this needs to reference "userStatus" somehow instead of Meteor.users.findOne(...), but since global object was not created, e.g. like UsersOnline = new Mongdb.Collection - I don't know how to implement this.

Getting error in Chrome debugging tool: TypeError: Cannot read property 'status' of undefined, obviously, it only gets currently logged in user.

Edit: To clarify: I get other users from a custom "profiles" collection in Mongo that I created specifically to overcome the issue of not being able to read "users" collection, due to security, I guess. So I get the list of profiles and supply the user name to this function that supposed to use mizzao package to see if they are online. Since the package reads from "users" collection, it only returns current user (me).

I would be totally OK if I could modify the package for it to write to my "profiles" collection, if someone suggests how to do that.

Another update: I ran the count on the server, and it returns only one (me, I guess). var cursor = Meteor.users.find({ "status.online": true }, { fields:{ _id: 1, status: 1, username: 1 // Any other fields you may need } }); console.log(cursor.count());

2

2 Answers

1
votes

If you have a publication like this:

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

it will return users that are online only. So you get undefined for users other than you, because they are not logged-in.

Try logging in as a different user at the same time (i.e. using different browser), and see whether you can now see the status of that user.

You can always change your publication to:

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

and you will get statuses for all users. But if you only need to check the "online" flag (i.e. you don't need other features of the package), it is better to only send data about the online users and consider not published user as offline.

0
votes

I guess you trying to read users before subscription is ready.

Server:

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

Client (you should return cursor, which will re-run once subscription is ready):

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