【问题标题】:.findOne works in data attribute of Iron Router Route but .find does not?.findOne 适用于 Iron Router Route 的数据属性,但 .find 不适用?
【发布时间】:2015-02-02 08:19:57
【问题描述】:

我正在编写一个具有动态内容加载的应用程序。我正在使用 Iron Router,并且如果可能的话,我的目标是避免使用 Sessions。我在下面写了以下路线:

Router.route('/:publisher',{
  name: "publisher",
  action: function(){
    this.render("Publisher")
  },
  data: function(){
    return Comics.findOne({publisher: this.params.publisher});
  }
});

这很有效,因为它使用 .findOne。如果我将 .findOne 切换到 .find,则不会加载任何内容,但不会出现错误。任何帮助表示赞赏。谢谢

注意:我查看了这个链接,但很遗憾不是同一个问题:findOne works but not get all/find

【问题讨论】:

  • 尝试使用var finde = Comics.find({publisher: this.params.publisher}).fetch();console.log(finde)
  • 这会产生多个数组,这似乎是在不断地循环,每次都在数组中添加一个条目。然后显示三个不同的数组,而不是附加原始数组。

标签: javascript meteor iron-router


【解决方案1】:

使用collection.find().fetch() 代替collection.find()

collection.findOne() 约等于collection.find({},{limit: 1}).fetch()[0]

说明

collection.find() 是一个游标,而collection.find().fetch() 是一个对象数组。

【讨论】:

  • 如果我控制台日志 Comics.find({publisher: this.params.publisher}).fetch(),三个数组是一个结果。看起来好像它正在使用这些参数遍历集合并每次创建一个新数组,将新的查找添加到其中。
  • 在订阅完成之前渲染模板吗?
  • 我忘了添加一个waitOn。傻我。谢谢!
猜你喜欢
  • 2016-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 2021-06-07
  • 2016-11-25
  • 2013-01-24
  • 1970-01-01
相关资源
最近更新 更多