0
votes

I would like to iterate over a Mongo query for the user's collections using the each block.

I have the following html template that should load in different pieces of data from the profile object in the users collection.

To clarify, the users Collection has a services, status and profile object

    {{#each profile}}
        <div class="profileUser oneDiv">
            <div class="profileUserLeft">
                <div class="profileUserImage">
                    <div class="spin"> {{> spinner}} </div>
                    <img src="{{profile.picturelrg}}" class="profileUserImg">
                </div>
                <div class="profileUserGraph">
                    <label for="myChart"><b>Meetup Graph</b>
                    <br>
                    <span class="profileMonth"> {{profile.month}} </span>
                    <br>
                        <canvas id="myChart"></canvas>
                    </label>
                </div>
            </div>
            <div class="profileUserRight">
                <div class="profileUserName">
                    <ul>
                        <li><h1>{{profile.name}}</h1></li>
                        <li>
                            <div class="circle" style="background-color: {{online.color}}"></div>
                        </li>
                    </ul>
                </div>
        </div>
    </div>
   {{/each}}

Here is my helper that sets the query

profile: function() {
            return Meteor.users.find({
                _id: id
            });
        }

Currently the page loads in no data.

When I statically query for a property however it works. This is done like so.

profimg: function() {
            return Meteor.users.find({
                _id: id
            }).fetch()[0].profile.picturelrg;
        }

How can I be more efficient and use the each block instead of statically searching for each different property utilizing the fetch() method?

1

1 Answers

1
votes

each of Blaze takes an array as parameter to loop while find method return a Cursor of MongoDB. What you need to do is fetch the Cursor to return the array

profile: function() {
    return Meteor.users.find({
        _id: id
    }).fetch();
}

However, your logic is not correct. You are finding the profile that matches with the input id, thus the function should be

profile: function() {
    return Meteor.users.findOne({
        _id: id
    });
}

and then you can access the property without the each loop