0

I'm currently working on an application, wherein which I've build out chat functionality.

I currently have a very limited way to create conversations, during which the user creating it, and the designated target user both have their ID's put into a Conversation document within a Conversations collection.

Im currently trying to loop through the array containing each of these ID's, and upon finding the ID that doesn't match the current users, to grab the other users profile information.

otherUserName: function(){
    for ( i = 0; i < this.users.length; i++ ) {
        if( this.users[i].id !== Meteor.user()._id){
            return( Meteor.users.findOne({ _id: this.users[i].id }));
        }
    }
}

Currently however, it doesn't output anything. It only shows up as undefined.

Any clue what I need to do to get the name to show up?

Jordan Whalen
  • 268
  • 2
  • 12
  • 1
    A few questions: have you published (and subscribed to) the users collection? Is `this.users[i].id` defined within the loop? Do you really want to `return` as soon as you hit *one* different id in your for loop? – SylvainB Apr 29 '15 at 08:41
  • I currently have autopublish on, and I do! There are only ever two users, so I only need to return once it finds the one that isnt the current user. – Jordan Whalen Apr 29 '15 at 08:47
  • 1
    When you enter `Meteor.users.find()` or `Meteor.user();` in the Chrome/browser console, what is the result? – Michael K. Apr 29 '15 at 09:07
  • 1
    The only reason I can imagine then is that `this.users[i].id` somehow contains either `null` or `undefined` or an incorrect value. Apologies for the obvious advice, but you could check using the `meteor mongo` client if the IDs you are looking for *do* exist. Also, check what type of ID (ObjectID or String) you are giving to your findOne method. – SylvainB Apr 29 '15 at 09:08
  • If I type Meteor.users.find() in the browser console, I get back a LocalCollection.cursor, and I can pass the current user Id to limit it to that one user, and it returns fine. If I pass it another user ID though, even one that I know exists, it fails. – Jordan Whalen Apr 29 '15 at 09:25
  • What does `Meteor.users.find().fetch()` give you? If it's only the current user and you're dead certain that `autopublish` is on, then I would assume you have only one user in database. – SylvainB Apr 29 '15 at 09:40
  • I figured it out! Apparently this.users[i].id wasnt returning a string. Once I converted it to a string, it worked. – Jordan Whalen Apr 29 '15 at 10:46
  • 1
    Yep, ObjectID it was then :) – SylvainB Apr 29 '15 at 11:23

2 Answers2

0

I have a similar problem, but on server side. When you need the user collection on the client, you have to publish it and subscribe to it.

I have a publication.js on server side with

Meteor.publish("users", function () {
    return Meteor.users.find();
});

And a router.js in a lib folder (for both client and server) with

Router.configure({
    layoutTemplate: 'layout',
    loadingTemplate: 'loading',
    notFoundTemplate: 'notFound',
    waitOn: function() { return [Meteor.subscribe('events'), Meteor.subscribe('users')]; }
 });

The users collection should be accessible in the client now. Maybe try to restrict the publish function to a couple properties and not all of them.

Community
  • 1
  • 1
Michael K.
  • 535
  • 6
  • 21
  • I guess that you still have to publish users, because it's a "special" collection, but that's out of my reach at the moment. – Michael K. Apr 29 '15 at 08:52
  • 2
    @Deftoned Not according to the docs: "If the autopublish package is installed, information about all users on the system is published to all clients." – SylvainB Apr 29 '15 at 09:02
0

The answer was all in converting the returned value to a string:

otherUserName: function(){
    for ( i = 0; i < this.users.length; i++ ) {
        if( this.users[i].id !== Meteor.user()._id){
            var user = String(this.users[i].id);
            return Meteor.users.findOne({ _id: user }).profile.name;
        }
    }
},
Jordan Whalen
  • 268
  • 2
  • 12