Edward
Edward

Reputation: 57

List array object value on view from Meteor user collection

I have following Meteor user account document:

{
    "_id":"aaaa",
    "profile":{
        "friendlist":[
            {"userId":"bbbb"},
            {"userId":"cccc"},
            {"userId":"dddd"}
        ]
    }
}

I want to iterate userId from friendlist array on my view which would look like this:

Friends List: bbbb, cccc, dddd

The html would look i suppose like following:

<template name="friendListCard">
    Friends List: 
    {{#each searchFriendList}}
        {{profile.friendlist.userId}},
    {{/each}}
</template>

and the template.helper like the following:

Template.friendListCard.helpers({
    searchFriendList: function(){
        Meteor.users.find({_id:Meteor.userId()})
    },
});

I just can't seem to make this happen. Any help?

Upvotes: 1

Views: 101

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20227

You need to return the array from your helper and then iterate over it:

<template name="friendListCard">
Friends List: 
{{#each searchFriendList}}
  {{userId}},
{{/each}}
</template>

js:

Template.friendListCard.helpers({
  searchFriendList: function(){
    return Meteor.users.findOne(Meteor.userId()).profile.friendlist;
  },
});

Also from a modeling pov, if you only have the userId in your friendlist array then you can just have an array of strings instead of an array of objects.

friendlist: ["bbbb","cccc","dddd"]

Upvotes: 1

Related Questions