Iron Router onBeforeAction is not called
I have a route setup /user
that should render the template login
if the current user is not logged in. The entire router has a waitOn
pending subscription currentUser
. End. The problem is, when I go to /user
, it just renders the template dataNotFound
.
Here are some code snippets that are relevant to this situation. I've tried to show you them in the order they are defined in my file lib/router.js
.
Router.plugin('dataNotFound', {notFoundTemplate: 'notFound'});
Router.onBeforeAction(function () {
console.log(Meteor.userId())
if (!Meteor.userId()) this.render('login');
else this.next();
}, { only: ['user'] });
Router.configure({
waitOn: function () { return Meteor.subscribe('currentUser'); }
});
Router.route('/user', {
name: 'user',
template: 'userView',
data: function () { return Meteor.user(); }
});
This console.log
above doesn't even work. It seems to me that since it should be a reactive function that even if initially rendered dataNotFound
, then shortly thereafter, the onBeforeAction
template should be run and rendered login
, right?
source to share
I had a problem with the onBeforeAction hook after upgrading from meteor 0.9.1 to 1. It didn't work when it should. For example, after logging out, I enter the address manually, and instead of the login page, I see the actual site waiting for data that never appears. The onRun hook solved this, but as the docs says, it only runs once.
Router.onRun(function () {
if (!Meteor.userId()) {
this.render('login');
} else {
this.next();
}
}, { only: ['user'] });
source to share
It's very strange that your console log doesn't even fire. I have a few ideas, but first I want to turn to the last part of your question.
The dataNotFound plugin is triggered when the data function is triggered in your route. This means it bypasses your onBeforeAction hook altogether, not that it doesn't get there.
One thing I can think of that might be worth trying would be to wrap the "custom" route action in a statement if ( this.ready() )
:
change:
Router.route('user', {
// Your other stuff
action: function() {
if this.ready() {
this.render();
},
The reason I suggest this is because you are using the waitOn operator, but I'm not 100% sure how this works if you don't have this.ready () somewhere in your route, because then, which (again, from my reading of the documentation, didn't mess with it) tells the router what to wait before executing. Perhaps this is not waiting now.
source to share
Try replacing Meteor.userId () with Meteor.user () in your if statement. See this link for help on how to handle user validation before the filter: http://www.manuel-schoebel.com/blog/meteorjs-iron-router-filters-before-and-after-hooks .
source to share
I found the problem here.
According to this post , thanks to Steeve Cannon. The problem is the function waitOn
.
Perhaps when you logginOut the subscription currentUser
will fail, causing your application to wait indefinitely. onBeforeAction
works afterwaitOn
You will need to check all the variables in you. Post to complete waitOn.
source to share
Place the operator if
inside the function waitOn
.
waitOn: function () {
if(Meteor.userId())
return Meteor.subscribe('currentUser');
}
Please refer to this comment for why this is happening: https://github.com/iron-meteor/iron-router/issues/1010#issuecomment-72210587
source to share