0

I have this code

/* Template Helpers Start */
    Template.ParticipationList.helpers({
        getAllParticipants: function(){
        var activityid = this._id;

        Meteor.call('participation.findParticipants', activityid, function(error, result){
            if(error){
                console.log(error)
            }
            return result;
        });

        }
    });

If I am placing the array and the return keyword outside the meteor call, all things are fine - it binds the names to the template. BUT when is the above, nothing binds to the template. I think that it has something to do with async... But what am I doing wrong.

UPDATE

 <ul>
    {{#each getAllParticipants}}
        {{name}}
    {{/each}}
</ul>
K N
  • 279
  • 5
  • 22
  • A [nearly identical question](http://stackoverflow.com/questions/40703704/meteor-helpers-return-nothing) was asked only a few hours ago. – MasterAM Nov 20 '16 at 20:09
  • So there were no solution to the problem. It was said that it was a scoping issue, but no solution was given. – K N Nov 20 '16 at 20:21
  • Not really. It all boils down to your design. There is not much use in having a method call per helper invocation. If you need to run a computation to get some state, there are probably better ways of doing it. Methods were not designed for data fetching. If you want to do it, you can have the computation populate a reactive dictionary that is bound to the template, for example. – MasterAM Nov 20 '16 at 20:25
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Quentin Nov 20 '16 at 20:29

1 Answers1

1

The "result" won't be in data. This is regular async programming. What do you want to do with your data ? You can put it in a reactive variable for other parts of your app to use.

Meteor.call('myMethod', functoin(error, result){
  if(error){
    alert('Error');
  }else{
    Session.set("data", result)
  }
});

I imagine you want to display documents from a collection say participants

You can than create a publication allowing certain fields.

e.g

Meteor.publish('allPartipants', function() {
  return Listings.find(
    {type: 'actors'},
    {fields: { name: 1, age: 1} }
  );
});

Subscription be like:

 Meteor.subscribe('allPartipants');

and in helper you just have to call the collection with find().fetch() as follows.

Template.templatName.helpers({
   allParticipants(){
    Participants.find().fetch()
  }
})

and your html as you mentioned

 <ul>
    {{#each allParticipants}}
        {{name}}
    {{/each}}
</ul>
hafiz ali
  • 1,378
  • 1
  • 13
  • 33
  • I would like to return the result, so that I can bind the data to the Template. See the update above for template markup. – K N Nov 20 '16 at 20:06
  • you should probably subscribe the collection and publish only the data required. and than instead of making meteor.call jst do Participants.find().fetch() you might not be able to activity id unless you each has already some scope of data. – hafiz ali Nov 20 '16 at 20:21