0

I'm stuck so any help really appreciated. I've tried lots of things - new to meteor and can't get the #each in template hand to show anything

See my meteorpad http://meteorpad.com/pad/FqMJWySAMZGcyaTf6 or see code below.

<template name="player">
    <div>{{_id}}</div>
    <div class="name">{{name}}</div>
    <div class="score">{{score}}</div>
    <div>{{>cards  hand}}</div>
</template>

<template name="cards">
<div>
{{#each hand}}
    <span>{{this}}</span>
{{/each}}
</div>
</template>

On client - response is showing correctly in console.log below:

Template.cards.hand = function(){
    if (Players.find().count() > 0 )
    {
    Meteor.call("deal", playerNum,function(err,response){
        if(err){
          console.log("error dealing: " + err);
        }
        console.log("in player hand" + response);

        return response;

    });
    }
  };
aledalgrande
  • 5,167
  • 3
  • 37
  • 65
  • Where do you get playerNum? – Walter Zalazar Sep 18 '14 at 01:10
  • The `Meteor.call` on the client is implemented internally as an `AJAX` operation, that's why it takes a call back `function(err,response)` to call later when the operation is complete. But that won't return anything to `Template.cards.hand()`, as the operation is merely queued `Template.cards.hand()` will return immediately with no data. – Paul Sep 18 '14 at 01:11
  • @Paul Asynchronous, but not AJAX. Meteor method calls use DDP (just like subscriptions). – Neil Sep 18 '14 at 01:20
  • possible duplicate of [How to get Meteor.Call to return value for template?](http://stackoverflow.com/questions/10677491/how-to-get-meteor-call-to-return-value-for-template) – Paul Sep 18 '14 at 01:22
  • @Neil Fine, but similar limitations. – Paul Sep 18 '14 at 01:23
  • I think one way to solve the issue is to have the hand stored in a session variable, and use the callback to Meteor.call update that session variable, which would trigger a redraw of a template if the template read from there. This involves restructuring more code than has been revealed. – Paul Sep 18 '14 at 01:24
  • maybe this is helpful https://github.com/arunoda/meteor-ddp-analyzer – sites Sep 18 '14 at 01:46
  • Thanks everyone - missed seeing answers til now - thought I set up email notification – user3538257 Oct 20 '14 at 20:59

1 Answers1

0

The problem is that you return a function, which cannot be a parameter of #each (note the console warning: Uncaught Error: {{#each}} currently only accepts arrays, cursors or falsey values.).

#each can only iterate on arrays or Cursors returned by meteorCollection.find

So instead of returning the function Alphas1 you should return:

return Alphas1(); // which in turn returns Alphas

or directly:

return Alphas;

See updated example

Matyas
  • 13,473
  • 3
  • 60
  • 73