来自游标助手的流星数据上下文

Meteor data context from cursor helper

我需要显示游标返回的某个数据字段。根据我的阅读,最好的方法是定义一个助手,然后使用 #each 标签

将数据访问到 html

当我在助手内部使用 findOne() 时,我得到了正确的数据结果,但当然只能从一个文档中得到,如果 findOne() 的代码是如下所示?(html 中的数据上下文位于 table 单元格中)。 findOne 代码但在 html 中仅返回一个文档字段是:

Template.products.helpers({
  getOffers (){
    result = OffersAggregate.findOne('offeringType-grouping').result[0].offeringType
    return result
  }
});

html代码:

 <td>{{getOffers }}<td>

find() 游标助手但错误结果:

"Exception in queued task Error: Can't call non-function:"

Template.products.helpers({
  getOffers (){
    return OffersAggregate.find().fetch()
  }
});
            <table id="table" class="table table-hover table-bordered">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Price</th>
                        <th>Country</th>                                
                        <th>Number of Offers</th>
                        <th>Offers</th>
                    </tr>
                </thead>
                <tbody>     
                    {{#each Product in Products}}   

                        <tr>
                            <td>{{Product.Name}}</td>
                            <td>{{Product.Price}}</td>
                            <td>{{Product.Country}}</td>
                            <td>{{Product.Number}}</td>
                            {{#each  offer in getOffers}}
                                {{#each result in offer.result  }}  
                                    <td>{{result.offeringType}}</td>
                                {{/each}}
                            {{/each}}   
                        </tr>
                    {{/each}}                           
                </tbody>
            </table>

如果我正确理解了您的 OffersAggregate 文档,则以下内容应该有效:

模板助手:

Template.products.helpers({
  getOffers (){
    return OffersAggregate.find().fetch();
  }
});

注册新的模板助手:

Template.registerHelper('returnValueByIndex', function (array, index){
   return array[index];
});

显示:

<table id="table" class="table table-hover table-bordered">
<thead>
 <tr>
  <th>Name</th>
  <th>Price</th>
  <th>Country</th>
  <th>Number of Offers</th>
  <th>Offers</th>
 </tr>
</thead>
<tbody>
 {{#each Product}}
 <tr>
  <td>{{Name}}</td>
  <td>{{Price}}</td>
  <td>{{Country}}</td>
  <td>{{Number}}</td>
  <td>
  {{#with returnValueByIndex Product.[0].getOffers.[0].result @index}}
   {{this.offeringType}}
  {{/with}}
 </td>
 </tr>
 {{/each}}
</tbody>

注意:感谢 Mo A 的回答,因为这个回答排在第一位。

{{#each}} 的上下文需要一个 iterable,它可以是一个数组或一个游标(或者像 nullundefined 这样的虚假值来跳过每个-块)。

阅读:http://blazejs.org/guide/spacebars.html#Each-and-With

函数Collection.findOne 立即对一个集合returns 一个文件。这是 Meteor 中 Collection.find().limit(1).fetch()[0] 的便捷快捷方式。

因此,对于您的用例,find() 需要使用正确的过滤器。您描述的错误与 find 无关,但与模板代码有关:

{{#each getOffers}}
  <!-- this will be inside a blick a reference to the current element -->
  {{#each this.result}}
    <!-- this is now refering to the result property on the current element -->
    {{#each offeringType}}<td>{{this}}</td>{{/each}}
  {{/each}}
{{/each}}

你只需要考虑一下,在 eachwith 块中, this 属性指的是当前文档。为了让你的 each 更容易推理你可以使用 each var in iterable:

{{#each offer in getOffers}}
  {{#each result in offer.result}}
    {{#each offeringType in result}}<td>{{offeringType}}</td>{{/each}}
  {{/each}}
{{/each}}

更多阅读:http://blazejs.org/guide/reusable-components.html(第 "Prefer {{#each .. in}}" 部分)