Getting a list of all registered users on Meteor

4,594 views
Skip to first unread message

NS_fib($val)

unread,
Nov 30, 2014, 10:02:55 PM11/30/14
to meteo...@googlegroups.com
I am attempting to get a list of all the users (just email/username/createdAt values), but I've been banging my head against the wall all day, and I can't get this to work despite having done something similar with a Collection I created in Meteor (client/server collection).

I've added meteor-accounts package, and I've even added the meteor-accounts-ui package to use it for testing.

I have a template in client/list.html that looks like:

<template name="listAll">
  {{#each user}}
      {{_id}}
      {{username}}
      {{emails.address[0]}}
  {{/each}}
</template>



in my helpers file client/list.js  I have:

Template.listAll.helpers({
  user
: function(){
     
return Meteor.users.find();
 
}
});




What am I missing?  I've also tried adding meteor publish and subscribe to the code, but I get errors.   Any help would be greatly appreciated.


NOTE:  Also, it seems when I replace
Meteor.users.find()

 with
Meteor.users.find().count()

 and the HTML template portion with just a
<template name="listAll">
  {{user}}
</template>

I get just the value "1", despite having registered over 5 accounts for testing.

Dan

unread,
Nov 30, 2014, 10:46:40 PM11/30/14
to meteo...@googlegroups.com
Try adding this.

if (Meteor.isClient) {
 
Meteor.subscribe('allUsers')
}


if (Meteor.isServer) {
 
Meteor.publish('allUsers', function() {
   
return Meteor.users.find({}, {fields:{username:1,emails:1}})
 
})
}

NS_fib($val)

unread,
Dec 1, 2014, 12:36:03 AM12/1/14
to meteo...@googlegroups.com
That made the
Meteor.users.find().count()

reflect the correct number of users.   However, in the main html template (listAll)  none of these handlebars work:

{{#each user}}
 
{{emails}}        <<----- returns [Object object]
 
{{emails.address}}   <<------ returns nothing
 
{{emails.address[0]}}  <<------ Makes meteor poop itself.
{{/each}}

NS_fib($val)

unread,
Dec 1, 2014, 12:54:14 AM12/1/14
to meteo...@googlegroups.com
Forgot to mention, that {{username}} and {{_id}} work fine, so I guess it's just that I don't know how to retrieve the email address stored in the collection.

Petar Korponaić

unread,
Dec 1, 2014, 6:23:15 AM12/1/14
to meteo...@googlegroups.com
emails[0].address

NS_fib($val)

unread,
Dec 1, 2014, 1:33:08 PM12/1/14
to meteo...@googlegroups.com
Oddly enough, I get an exception error when I do this, and I do have an email address defined.

When I perform the following commands in the Web console (Safari), I get the following, which shows me I have the data available to the client:

> Meteor.users.find()

< Object
  _selectorId
: undefined
  _transform
: null
 
>collection: Object
  fields
: undefined
  limit
: undefined
 
>matcher: Object
  reactive
: true
  skip
: undefined
  sorter
: null
 
>__proto__: Object


findOne shows me actual fields of a given user account:

> Meteor.users.findOne()

< Object
  _id
: "cZjCoDHgBe9ma2nA9"
 
>emails: Array[1]
  username
: "Meema"
 
>__proto__: Object



However, when I try to use what was suggested, I get the following exception:

emails[0].address

Exception from Tracker recompute function: Can't call non-function: [object Object]

... (it goes on... I am just shortening it here for sake of brevity)
Exception in queued task: http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2525:37
nonreactive@http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:502:13
addedAt@http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2510:28
addedAt@http://localhost:3000/packages/observe-sequence.js?2fd807ea171ead273b9e6458607cb226012d9240:339:26
addedBefore@http://localhost:3000/packages/minimongo.js?9adf0102ce60b3df01914155826e1661f636e91d:3839:35
addedBefore@http://localhost:3000/packages/minimongo.js?9adf0102ce60b3df01914155826e1661f636e91d:3776:60
http://localhost:3000/packages/minimongo.js?9adf0102ce60b3df01914155826e1661f636e91d:420:18
runTask@http://localhost:3000/packages/meteor.js?61916b1060b33931a21f104fbffb67c2f3d493c5:688:12
flush@http://localhost:3000/packages/meteor.js?61916b1060b33931a21f104fbffb67c2f3d493c5:716:17
drain@http://localhost:3000/packages/meteor.js?61916b1060b33931a21f104fbffb67c2f3d493c5:724:17
insert@http://localhost:3000/packages/minimongo.js?9adf0102ce60b3df01914155826e1661f636e91d:628:27
update@http://localhost:3000/packages/mongo.js?9bc2c5a8b2796fab86b51660ca643e5a49a30c84:252:34
http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:3908:53
http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:4658:25
forEach@[native code]
forEach@http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:156:18
http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:4657:17
forEach@http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:26
_livedata_data@http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:4654:13
onMessage@http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:3714:26
http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:2713:19
forEach@[native code]
forEach@http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:156:18
onmessage@http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:2712:15
dispatchEvent@http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:152:27
_dispatchMessage@http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:1137:23
_didMessage@http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:1195:34
onmessage@http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:1342:28

Petar Korponaić

unread,
Dec 1, 2014, 1:44:10 PM12/1/14
to meteo...@googlegroups.com
You cannot write {{emails[0].address}} directly. 

Make helper instead:

Template.YOUR_TEMPLATE_NAME.helpers({
  "userEmail": function() {  return this.emails[0].address; }
});

And in your html use:

{{userEmail}}


That's it :)


--
You received this message because you are subscribed to a topic in the Google Groups "meteor-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/meteor-talk/TrMjDDaThbs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to meteor-talk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/meteor-talk/a3dc5023-ba9a-40f9-836a-87cf2566610c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

NS_fib($val)

unread,
Dec 1, 2014, 2:11:01 PM12/1/14
to meteo...@googlegroups.com
Fair enough, I appreciate the help.  Thank you to both for the help.

I added an extra helper and it works as you suggested Petar:

Template.listUs.helpers({

  user
: function(){
   
return Meteor.users.find();
 
}
});


Template.listUs.helpers({

  userEmail
: function(){
   
return this.emails[0].address;
 
}
});

I also have in my HTML:

<template name="listUs">
  {{#each user}}
    {{userEmail}}
  {{/each}}
</template>



On a side note, is there a way to combine those into one Template.listUs.helpers?  Such as the following, which I know is incorrect:

Template.adminMod.helpers({
  user
: function(){
   
return Meteor.users.find();

NS_fib($val)

unread,
Dec 1, 2014, 2:20:15 PM12/1/14
to meteo...@googlegroups.com
I spoke a little too soon, in the solution above, I get a listing of usernames, but email only for the last user in the list:

Albert Smith
John Smith
Bob Smith   bobs...@email.com

Code:
JS

Template.listUs.helpers({

  user
: function(){
   
return Meteor.users.find();
 
}
});

Template.listUs.helpers({

  userEmail
: function(){
   
return this.emails[0].address;
 
}
});


HTML:
<template name="listUs">
  {{#each user}}
    {{username}} {{userEmail}}
<br>
  {{/each}}
</template>


Kelly Copley

unread,
Dec 1, 2014, 4:35:21 PM12/1/14
to meteo...@googlegroups.com

Sounds like you haven't published that data.. still relying on auto publish to publish the data? In which case you would only have the email address for the current logged in user.

Petar Korponaić

unread,
Dec 1, 2014, 4:46:02 PM12/1/14
to meteo...@googlegroups.com
You can combine any number of helpers into single Template.XXX.helpers({ }) by separating helpers with comma :) So in your example:

Template.adminMod.helpers({
  user: function(){
    return Meteor.users.find();
  },

  userEmail: function(){
    return this.emails[0].address;
  }


});


I believe you are just started learning Meteor, so I suggest you to build each of Meteor's built-in examples and carefully review the code. 

Also, you started fighting with user account system that is non-trivial because security mechanisms, so your first contact with meteor can be little bit frustrating but don't give up!

:)   


--
You received this message because you are subscribed to a topic in the Google Groups "meteor-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/meteor-talk/TrMjDDaThbs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to meteor-talk...@googlegroups.com.

NS_fib($val)

unread,
Dec 2, 2014, 4:07:05 PM12/2/14
to meteo...@googlegroups.com
Yes, actually fairly new to front-end development, and some of the new tools have some of that "magic" behind the scenes that I need to familiarize myself with.  Thank you for the help, again, I got this issue worked out.  Although I have another one, and I'll create a new thread about it.

I used the following on the server/publish.js

  Meteor.publish('allUsers', function(){
   
return Meteor.users.find({}, {fields: {username: 1, emails: 1}})

 
});


and I subscribed to it in the client/subs.js portion.


Here's an odd thing, if I add the profile field, it doesn't show up, I get have the console show the user object, with the following:

  Meteor.publish('allUsers', function(){
   
return Meteor.users.find({}, {fields: {username: 1, emails: 1, profile: 1}})
 
});


// Inside a template helper:


   
var Ul = Meteor.users.findOne({_id: this._id});
    console
.log(U);


The object looks like this without profile: (web console)

>Object
_id
: "A3KpDzrf3TvkvFBNS"
>emails: Array[1]
username
: "testing2"
__proto__
: Object
accounts
.js:56


_George

unread,
Jan 17, 2015, 12:39:15 PM1/17/15
to meteo...@googlegroups.com
Wow. Thank you for this thread. I think I looked at everything else on the internet related to this and nothing else was correct. 

NS_fib($val)

unread,
Jan 20, 2015, 9:58:23 PM1/20/15
to meteo...@googlegroups.com
Awesome :)  The group here is really helpful.
Reply all
Reply to author
Forward
0 new messages