Meteor and Iron Router: raise 404 when id does not exist

I am 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 region of _id . However, when I access http://example.com/region/whatever , the page displays fine, but without data.

How can I raise 404 error for this?

+7
meteor iron-router
source share
4 answers

not 404, but you can display the page not found 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; } }); 
+8
source share

I think you can try Plugins

According to this documentation, there is already a built-in plugin for this problem.

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

And it is described as

This ready-made plugin will automatically display a template called "notFound" if the route data is false

+5
source share

In router.js

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

Configure the router to solve the problem.

and part of the template

 <template name="notFound"> <div class="container"> <h1 class="colorWhite">Oopss..ss..ss..</h1> <p class="colorWhite">You are out of the world, let go back</p> </div> </template> 

do this: it should work, actually it works for me ..

0
source share

I include the entire 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 pageNotFound then displayed in the {{> yeild}} the application template if none of the other routes takes the uri path.

-one
source share

All Articles