【问题标题】:#each loop {{this }} not getting populated#each 循环 {{this }} 未填充
【发布时间】:2014-11-12 04:47:17
【问题描述】:

我被困住了,所以非常感谢任何帮助。我已经尝试了很多东西 - 对流星来说是新手,但无法让模板中的 #each 显示任何内容

查看我的流星垫http://meteorpad.com/pad/FqMJWySAMZGcyaTf6 或查看下面的代码。

<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>

在客户端 - 响应在下面的 console.log 中正确显示:

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;

    });
    }
  };

【问题讨论】:

  • 从哪里获得 playerNum?
  • 客户端上的Meteor.call在内部实现为AJAX操作,这就是为什么它需要回调function(err,response)以便稍后在操作完成时调用。但这不会向Template.cards.hand() 返回任何内容,因为操作只是排队,Template.cards.hand() 将立即返回,没有数据。
  • @Paul 异步,但不是 AJAX。 Meteor 方法调用使用 DDP(就像订阅一样)。
  • @Neil Fine,但有类似的限制。

标签: templates meteor each


【解决方案1】:

问题是你返回一个函数,它不能是#each的参数(注意控制台警告:Uncaught Error: {{#each}} currently only accepts arrays, cursors or falsey values.)。

#each 只能迭代 arraysCursors 返回的 meteorCollection.find

因此,您应该返回而不是返回函数Alphas1

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

或直接:

return Alphas;

See updated example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-12
    • 2019-08-01
    • 2017-12-08
    • 1970-01-01
    • 2019-12-30
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多