找到你要的答案

Q:Route using wrong controller?

Q:使用错误的控制器路由?

I have 2 controllers (iron-router), one for access bits (login etc.) and one for the logged in area. But for some reason one of my routes is choosing to use the wrong controller, even though I'm explicitly stating which one to use. Here is the code:

// 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: {
        …
      }
    });
  }
});

When I visit app/account I'm redirected to app/dashboard, as directed in the AccessController. Why is the app/account route using the wrong controller logic?

Edit: Oddly, if I remove the controller declaration in the offending route (controller: 'DashboardController') then the template loads fine. So it only uses the wrong controller when I ask it to us a controller.

I must be missing something but that's awfully odd.

我有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: {
        …
      }
    });
  }
});

当我访问的应用程序/帐户我重定向到应用程序/仪表板,按照存取控制器。为什么应用程序/帐户路径使用错误的控制器逻辑?

编辑:奇怪的是,如果我删除控制器声明违规的路线(控制器:“dashboardcontroller”)然后模板荷载精细。所以它只使用错误的控制器当我问我们一个控制器。

我一定错过了什么,但那太奇怪了。

answer1: 回答1:

I think that your problem comes from the fact that you are using Meteor.user() in both controllers, which is the actual user document. And like any other collection it may not be immediately ready when the application starts.

If you add a console.log(Meteor.user()) in your controllers, you will see that it is first briefly undefined before returning the user document. So the route is using the right controller but Meteor.user() is undefined so you are redirected to /app/login where Meteor.user() (probably ready now) returns the documents so you get redirected to /app/dashboard.

To prevent such behavior I use Meteor.userId() which is always available no matter what. And I only use Meteor.user() when I have first tested that Meteor.userId() returned something and if I need more information about the user.

我认为你的问题在于你使用的是流星,控制器user(),这是实际的用户文档。和任何其他集合一样,应用程序启动时可能不会立即准备好。

If you add a console.log(Meteor.user()) in your controllers, you will see that it is first briefly undefined before returning the user document. So the route is using the right controller but Meteor.user() is undefined so you are redirected to /app/login where Meteor.user() (probably ready now) returns the documents so you get redirected to /app/dashboard.

为了防止这样的行为,我用流星。userid()总是可用的无论什么。我只能用流星。user()当我第一次测试,流星。userid()回来的东西,如果我需要更多关于用户的信息。

meteor  iron-router