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?
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;
}
});
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With