7

I'm using Iron Router for my urls and I have this route:

this.route('regionEdit', {
  path: '/region/:_id',
  waitOn: function() {
    return Meteor.subscribe('region', this.params._id);
  },
  data: function() {
    return Regions.findOne({
      _id: this.params._id
    });
  }
});

This works fine when I use this path http://example.com/region/xgok3Etc5mfhtmD7j

Where xgok3Etc5mfhtmD7j is the _id of region. However, when I access to http://example.com/region/whatever, the page renders normally, but without data.

How can I raise a 404 error for this?

Richard de Wit
  • 7,102
  • 7
  • 44
  • 54
Braulio Soncco
  • 353
  • 1
  • 4
  • 20
  • 2
    This is talked about in iron-router issue [#237](https://github.com/EventedMind/iron-router/issues/237). It's possible to return a 404 response from the server but not from the client. – David Weldon May 16 '14 at 23:38
  • Possible duplicate of [How to return 404 using Iron Router](http://stackoverflow.com/questions/27001298/how-to-return-404-using-iron-router) – Cees Timmerman Oct 08 '15 at 11:08

4 Answers4

8

not a 404, but you can render a not found page by doing something like this.

this.route('regionEdit', {
  path: '/region/:_id',
  waitOn: function() {
    return Meteor.subscribe('region', this.params._id);
  },
  data: function() {
    var region = Regions.findOne({
      _id: this.params._id
    });
    if(!region)
      this.render("notFound");
    else
      return region;
  }
});
landland
  • 2,117
  • 3
  • 20
  • 26
5

I think you can try Plugins

According to this documentation there is already a built in plugin for this issue

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

And it is described as

This out-of-box plugin will automatically render the template named "notFound" if the route's data is falsey

Süha Boncukçu
  • 913
  • 1
  • 10
  • 29
  • 1
    I believe that this dataNotFound plugin has to do with template data, not with a missing route or template. – Justin McCandless Dec 09 '14 at 06:21
  • 1
    But @Braulio indicates the template is rendered correctly without data (which means data function returns false) if I am not missing anything. Anyway, I had the same case if I understand correctly and it worked for me. – Süha Boncukçu Dec 10 '14 at 10:32
  • 1
    You might be right. I'm not sure about the dataNotFound plugin, but I got it to work as a config parameter: `Router.configure({notFoundTemplate: '404'});` – Justin McCandless Dec 10 '14 at 20:06
  • 2
    The `Router.configure({notFoundTemplate: '404'});` template will only be rendered if a corresponding Route is not found. Since OP is using params, the route will be found, so will render, but with no data. – phocks Jun 17 '15 at 01:20
0

In router.js

Router.configure({
    layoutTemplate:'layout',
    notFoundTemplate: 'notFound'
});

Configure your router this way to solve the issue

and Template Part

<template name="notFound">
    <div class="container">

            <h1 class="colorWhite">Oopss..ss..ss..</h1>
            <p class="colorWhite">You are out of the world, let's go back</p>

    </div>
</template>

make it like this.. it should work, in fact, it works for me..

Mahesh Babu
  • 145
  • 13
-1

I include a catch all route on my Router.map:

router.js

Router.map(function() {

    ... // other routes

    this.route('404', {
       path: '/*',
       layoutTemplate: 'application', // this actually lives in Router.configure();
       template: 'pageNotFound',
       onBeforeAction: function(){
          console.log('not found');
       }
    });
});

templates.html

<template name="pageNotFound">
    <div>
        <h2>404 - page not found</h2>
    </div>
</template>

<template name="application">
    <div>
        <h1>My Application</h1>

        {{> yield}}

    </div>
</template>

This pageNotFound template then gets rendered in the {{> yeild}} partial of the application template if none of the other routes pick up the uri path.

Joshua
  • 6,320
  • 6
  • 45
  • 62