Search code examples
javascriptmeteoriron-router

.findOne works in data attribute of Iron Router Route but .find does not?


I am writing an app with dynamic content loads. I am using Iron Router and am aiming to avoid Sessions if possible. I wrote the following route bellow:

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

Which works, as it uses .findOne. If I switch .findOne to .find, nothing loads, but there are no errors. Any help is appreciated. Thanks

Note: I looked at this link, but it's sadly not the same problem: findOne works but not get all/find


Solution

  • Use collection.find().fetch() instead of collection.find()

    collection.findOne() is approximately equivalent to collection.find({},{limit: 1}).fetch()[0]

    Explanation

    collection.find() is a cursor, whereas collection.find().fetch() is an array of objects.