How do I find/return data in subdocuments?


#1

Sorry for another newbsie question, but I tried a lot of options and can’t seem to figure this out.

I have “Groups” collection organized like the one below. I want to be able to iterate through Members on the client, listing the Role entry and the Username entry for each using spacebar templates:

  {
          "Groupid" : "1",
          "Someoption" : true,
          "Members" : [{
               "Role": " whatever"
              "UserInfo" : {
                        "_id" : "6",
                        "username" : "Marcus"

I can get as far as iterating through members, but I’m having no luck grabbing the members.userinfo.username field. I realize this is pretty general, but I’ve tried lot of variations on syntax and I’ve not managed to come up with anything. Help would be appreciated incredibly.


#2

Hi, Members[n].userinfo.username.


#3

there was no “,” between “whatever” and “UserInfo” just sharing :smile:

and nxcong is right provide array index

Members[index].userinfo


#4

Ah thanks, I wasn’t even aware I could do this. I’m much closer to the right track now.

Naturally, I tried it first with just an n and hoped Meteor would somehow read my mind, but I suppose I need to do something with .count now, which is no big deal – unless there’s a simpler way to do that?

But thank you either way, that was the piece I needed.


#5

The comma problem was only in my attempt to simplify for sake of example, but thanks anyway and good catch – you never know. :smile:


#6

:smiley: yeah you never know :smiley:


#7

Actually, I apparently have no idea how to increment the array index as I loop through. :confused:


#8
{{#each Members}}
Role : {{Role}}, Id : {{userinfo._id}}, username : {{userinfo.username}}
{{/each}}

#9

Oh gosh, I should have thought of doing it this way before instead of stressing over indexes. Thank you so much, I was part the point of being able to think clearly. Working exactly as it should now.

:cookie: