使用错误的控制器路由?
Route using wrong controller?
我有 2 个控制器(铁路由器),一个用于访问位(登录等),另一个用于登录区域。但出于某种原因,我的一条路线选择使用错误的控制器,即使我明确说明要使用哪一个。这是代码:
// Controllers
AccessController = RouteController.extend({
layoutTemplate: 'AccessMaster',
onBeforeAction: function () {
if (Meteor.user()) { // If user is logged in then take them to the Dashboard
this.redirect('/app/dashboard');
} else {
this.next();
}
}
});
DashboardController = RouteController.extend({
layoutTemplate: 'DashboardMaster',
onBeforeAction: function () {
if (!Meteor.user()) { // If user is not logged in then take them to the login
this.redirect('/app/login');
} else {
this.next();
}
}
});
// Routes
Router.route("/app/signup", {
name: 'Signup',
controller: 'AccessController'
});
Router.route("/app/login", {
name: 'Login',
controller: 'AccessController'
});
Router.route("/app/account", {
name: 'Account',
controller: 'DashboardController',
loadingTemplate: 'Loading',
action: function () {
this.render('Account');
}
});
Router.route("/app/dashboard", {
name: 'Dashboard',
controller: 'DashboardController',
loadingTemplate: 'Loading',
waitOn: function () {
…
},
action: function () {
this.render('Dashboard', {
data: {
…
}
});
}
});
当我访问 app/account
时,我被重定向到 app/dashboard
,如 AccessController
中的指示。为什么 app/account
路由使用了错误的控制器逻辑?
编辑: 奇怪的是,如果我删除了有问题的路由 (controller: 'DashboardController'
) 中的控制器声明,那么模板可以正常加载。所以当我向我们询问它时它只会使用错误的控制器 a controller.
我一定是遗漏了什么,但这太奇怪了。
我认为您的问题是因为您在两个控制器中都使用了 Meteor.user()
,这是实际的用户文档。与任何其他集合一样,它可能不会在应用程序启动时立即准备就绪。
如果您在控制器中添加一个 console.log(Meteor.user())
,您会看到它在返回用户文档之前先短暂地 undefined
。
所以路由使用了正确的控制器但是 Meteor.user()
是 undefined
所以你被重定向到 /app/login
其中 Meteor.user()
(现在可能准备好了) returns 文件所以你被重定向到 /app/dashboard
.
为了防止这种行为,我使用了 Meteor.userId()
,它无论如何都可用。我只在我第一次测试 Meteor.userId()
返回了一些东西并且如果我需要有关用户的更多信息时才使用 Meteor.user()
。
我有 2 个控制器(铁路由器),一个用于访问位(登录等),另一个用于登录区域。但出于某种原因,我的一条路线选择使用错误的控制器,即使我明确说明要使用哪一个。这是代码:
// Controllers
AccessController = RouteController.extend({
layoutTemplate: 'AccessMaster',
onBeforeAction: function () {
if (Meteor.user()) { // If user is logged in then take them to the Dashboard
this.redirect('/app/dashboard');
} else {
this.next();
}
}
});
DashboardController = RouteController.extend({
layoutTemplate: 'DashboardMaster',
onBeforeAction: function () {
if (!Meteor.user()) { // If user is not logged in then take them to the login
this.redirect('/app/login');
} else {
this.next();
}
}
});
// Routes
Router.route("/app/signup", {
name: 'Signup',
controller: 'AccessController'
});
Router.route("/app/login", {
name: 'Login',
controller: 'AccessController'
});
Router.route("/app/account", {
name: 'Account',
controller: 'DashboardController',
loadingTemplate: 'Loading',
action: function () {
this.render('Account');
}
});
Router.route("/app/dashboard", {
name: 'Dashboard',
controller: 'DashboardController',
loadingTemplate: 'Loading',
waitOn: function () {
…
},
action: function () {
this.render('Dashboard', {
data: {
…
}
});
}
});
当我访问 app/account
时,我被重定向到 app/dashboard
,如 AccessController
中的指示。为什么 app/account
路由使用了错误的控制器逻辑?
编辑: 奇怪的是,如果我删除了有问题的路由 (controller: 'DashboardController'
) 中的控制器声明,那么模板可以正常加载。所以当我向我们询问它时它只会使用错误的控制器 a controller.
我一定是遗漏了什么,但这太奇怪了。
我认为您的问题是因为您在两个控制器中都使用了 Meteor.user()
,这是实际的用户文档。与任何其他集合一样,它可能不会在应用程序启动时立即准备就绪。
如果您在控制器中添加一个 console.log(Meteor.user())
,您会看到它在返回用户文档之前先短暂地 undefined
。
所以路由使用了正确的控制器但是 Meteor.user()
是 undefined
所以你被重定向到 /app/login
其中 Meteor.user()
(现在可能准备好了) returns 文件所以你被重定向到 /app/dashboard
.
为了防止这种行为,我使用了 Meteor.userId()
,它无论如何都可用。我只在我第一次测试 Meteor.userId()
返回了一些东西并且如果我需要有关用户的更多信息时才使用 Meteor.user()
。