Search code examples
meteoriron-router

Iron-router set notFoundTemplate when no data found in database


How can I set a layout when my data function returns null.

For example, in the route below, when chefs is null, I would like to render my 'notFound' template.

Router.route('/vendors/chefs/:_url', {
  template: 'chefs',
  data: function() {
    var chefs = Chef_db.findOne({url: this.params._url});
    return chefs;
  }
});

Solution

  • Take a look at the notFoundTemplate in the documentation: https://github.com/iron-meteor/iron-router/blob/devel/Guide.md

    You can apply it globally:

    Router.plugin('dataNotFound', {notFoundTemplate: 'notFound'});

    Or you can apply it to specific routes using the except/only options:

    Router.plugin('dataNotFound', {
      notFoundTemplate: 'NotFound', 
      except: ['server.route']
      // or only: ['routeOne', 'routeTwo']
    });