来自游标帮助器的Meteor数据上下文

来自游标帮助器的Meteor数据上下文,meteor,meteor-blaze,Meteor,Meteor Blaze,我需要显示从游标返回的某个数据字段。据我所知,最好的方法是定义一个助手,然后使用#each标记将数据访问到html中 当我在助手中使用findOne()时,我得到了正确的数据结果,但当然只能从一个文档中获得,如果findOne()的代码如下所示(html中的数据上下文在一个表单元格中),我怎么能对所有文档获得相同的结果呢。 findOne代码,但在html中仅返回一个文档字段: Template.products.helpers({ getOffers(){ 结果=OfferAggregate.

我需要显示从游标返回的某个数据字段。据我所知,最好的方法是定义一个助手,然后使用#each标记将数据访问到html中

当我在助手中使用
findOne()
时,我得到了正确的数据结果,但当然只能从一个文档中获得,如果
findOne()
的代码如下所示(html中的数据上下文在一个表单元格中),我怎么能对所有文档获得相同的结果呢。
findOne
代码,但在html中仅返回一个文档字段:

Template.products.helpers({
getOffers(){
结果=OfferAggregate.findOne('offeringType-grouping')。结果[0]。offeringType
返回结果
}
});
html代码:

{{getOffers}
find()
cursor helper,但有错误结果:

“队列任务中出现异常错误:无法调用非函数:”

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

名称
价格
国家
优惠数量
提供
{{#产品中的每个产品}
{{Product.Name}
{{Product.Price}}
{{Product.Country}
{{Product.Number}
{{{#getOffers中的每个报价}
{{#offer.result中的每个结果}
{{result.offeringType}
{{/每个}}
{{/每个}}
{{/每个}}

如果我正确理解了您的
报价汇总
文档,以下内容应该有效:

模板帮助程序:

Template.products.helpers({
  getOffers (){
    return OffersAggregate.find().fetch();
  }
});
Template.registerHelper('returnValueByIndex', function (array, index){
   return array[index];
});
注册新模板帮助程序:

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>

名称
价格
国家
优惠数量
提供
{{{#每种产品}
{{Name}}
{{Price}}
{{国家}
{{Number}}
{{#与returnValueByIndex乘积[0]。getOffers[0]。结果@index}
{{this.offeringType}}
{{/与}}
{{/每个}}

如果我正确理解了您的
报价汇总
文档,以下内容应该可以使用:

模板帮助程序:

Template.products.helpers({
  getOffers (){
    return OffersAggregate.find().fetch();
  }
});
Template.registerHelper('returnValueByIndex', function (array, index){
   return array[index];
});
注册新模板帮助程序:

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>

名称
价格
国家
优惠数量
提供
{{{#每种产品}
{{Name}}
{{Price}}
{{国家}
{{Number}}
{{#与returnValueByIndex乘积[0]。getOffers[0]。结果@index}
{{this.offeringType}}
{{/与}}
{{/每个}}

注意:由于Mo A的答案是第一个,所以要对其进行评分

{{{#each}
的上下文需要一个
iterable
,它要么是数组,要么是游标(或者像
null
undefined
这样的假值来跳过每个块)

阅读:

集合上的函数
Collection.findOne
立即返回文档。这是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}}

更多内容请阅读:(部分“preference{{{{{p}each..in}}”)

注:由于Mo A的答案是第一个,所以请对其进行评分

{{{#each}
的上下文需要一个
iterable
,它要么是数组,要么是游标(或者像
null
undefined
这样的假值来跳过每个块)

阅读:

集合上的函数
Collection.findOne
立即返回文档。这是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}}

更多内容:(部分“preference{{{{each..in}}”)

尝试“return OffersAggregate.find().fetch()”。关于#每个片段,你能确认你的'OffersAggregate'文档是什么样子吗?@Mo A我添加了一个屏幕截图,希望此帮助栏'return OffersAggregate.find().fetch()'。关于#each segment,你能确认你的'OffersAggregate'文档是什么样子吗?@moa我添加了一个屏幕截图,希望这有帮助在阅读、测试等之后,我最终得到了这个
{{{each getOffers}{{{each result}{{offeringType}{{/each}{
如上面更新的代码所示。我收到了正确的数据(TRAM”“BUS”等,如前所述),但在一个扩展嵌套单元中,该单元包含从游标检索到的所有数据,并将其放在表的一行中,而我需要每行/单元进行一次迭代,我尝试了一些修复,但没有解决。如果我有多个{{#each},该如何处理模板中的注释?我上面提供的代码片段通过“offeringType”迭代,以单独打印它的每个组成部分(即BUS,TRAM)。这对您不起作用吗?@user2829319您的代码遗漏了“{{{each offeringType}}{{{this}{/each}}}}”@Mo a我需要将“BUS,TRAM”放在一个单元格中,所以我故意遗漏了这个
{{{#每个提供类型}}{{this}{{{/每个}}
但现在的问题是,在每一行中,我都有该单元格中的所有迭代,这将导致一个包含所有可用数据的巨大单元格,而不是每一行。如果您可以提供一个您试图打印的内容的HTML示例,然后可以建议如何执行。经过一些阅读、测试等,我最终得到了该示例