【问题标题】:Session is not Defined - Meteor 1.4 and Flow Router会话未定义 - Meteor 1.4 和流路由器
【发布时间】:2016-08-19 05:49:51
【问题描述】:

我正在尝试使用 Flow Router 为我的 Meteor 应用程序实现自定义登录、授权和角色。我正在使用最新版本的 Meteor。我也更新了所有软件包。我登录时收到错误消息。

注册和创建帐户完美无缺。我的凭据已保存并已创建用户。

我在loggedIn.js 中设置会话变量,然后在单击登录按钮时在configure.js 中检索它,并且“应该”将我引导到我在登录之前尝试去的所需路线,但是我得到了什么是“会话未定义”错误。这是我收到的两个错误:

Exception in onLogin callback: TypeError: Cannot read property 'replace' of undefined
at Router.path (http://localhost:3000/packages/kadira_flow-router.js?hash=09ea12875d3801955ee70797bf8e4a70feebc570:325:18)
at Router.go (http://localhost:3000/packages/kadira_flow-router.js?hash=09ea12875d3801955ee70797bf8e4a70feebc570:360:19)
at http://localhost:3000/app/lib/routes/configure.js?hash=1ca98e9145d8b9d63189b16a8d872866175709b0:15:25
at runAndHandleExceptions (http://localhost:3000/packages/callback-hook.js?hash=fff7fdef0707c85900b21f766a4b6c65bf278ff4:162:24)
at http://localhost:3000/packages/callback-hook.js?hash=fff7fdef0707c85900b21f766a4b6c65bf278ff4:169:12
at http://localhost:3000/packages/accounts-base.js?hash=db584b046b0a64d03bfcbf1cd84a8b38f83ddc0d:290:9
at Hook.each (http://localhost:3000/packages/callback-hook.js?hash=fff7fdef0707c85900b21f766a4b6c65bf278ff4:138:15)
at http://localhost:3000/packages/accounts-base.js?hash=db584b046b0a64d03bfcbf1cd84a8b38f83ddc0d:289:25
at http://localhost:3000/packages/underscore.js?hash=27b3d669b418de8577518760446467e6ff429b1e:794:19
at loggedInAndDataReadyCallback (http://localhost:3000/packages/accounts-base.js?hash=db584b046b0a64d03bfcbf1cd84a8b38f83ddc0d:411:5)

从 cmd 行:

I20160818-21:36:28.962(-7)? Exception in onLogin callback: ReferenceError: Session is not defined
I20160818-21:36:29.266(-7)?     at app\lib\routes\configure.js:9:3
I20160818-21:36:29.267(-7)?     at runAndHandleExceptions      (packages/callback-hook/hook.js:133:1)
I20160818-21:36:29.267(-7)?     at packages/callback-hook/hook.js:140:1
I20160818-21:36:29.267(-7)?     at packages/accounts-   base/accounts_server.js:167:5
I20160818-21:36:29.267(-7)?     at [object Object]._.extend.each (packages/callback-hook/hook.js:109:1)
I20160818-21:36:29.267(-7)?     at AccountsServer.Ap._successfulLogin (packages/accounts-base/accounts_server.js:166:21)
I20160818-21:36:29.267(-7)?     at AccountsServer.Ap._attemptLogin (packages/accounts-base/accounts_server.js:356:10)
I20160818-21:36:29.267(-7)?     at [object Object].methods.login (packages/accounts-base/accounts_server.js:533:21)
I20160818-21:36:29.267(-7)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1711:12)
I20160818-21:36:29.267(-7)?     at packages/ddp-server/livedata_server.js:711:19

我已经多次删除并重新添加了 Sessions 包。这是我目前正在使用的所有软件包的列表:

less@2.7.4
twbs:bootstrap@3.3.6
fortawesome:fontawesome
jquery@1.11.9
okgrow:router-autoscroll
meteor-base@1.0.4
mobile-experience@1.0.4
mongo@1.1.10
blaze-html-templates@1.0.4
tracker@1.1.0
logging@1.1.14
reload@1.1.10
random@1.0.10
ejson@1.0.12
spacebars@1.0.12
check@1.2.3
kadira:flow-router
meteorhacks:fast-render
kadira:blaze-layout
zimme:active-route
arillo:flow-router-helpers
accounts-password@1.2.12
alanning:roles
accounts-base@1.2.9
standard-minifier-css
standard-minifier-js
meteortoys:allthings@3.0.0
session

到目前为止,我有 3 个用于流路由器的文件,用于我创建的不同路由。

configure.js 中,这是Session is not defined 错误开始的地方:

    Accounts.onLogin(function() { // This makes sure the user goes to the route that he wanted after he successfully logged in.

  var redirect = Session.get('redirectAfterLogin');
  console.log(redirect);
  if (redirect !== null) { // added this check here because some async behaviour in either FlowRouter or onLogin hook can cause wrong redirect to the ‘ login’ page again. This explicit check solves that issue.
    if (redirect !== '/login') {
      return FlowRouter.go(redirect);
    }
  }
});

loggedIn.js,我为登录的用户路由创建了组路由:

var loggedIn = FlowRouter.group({
  name: loggedIn,
  triggersEnter: [ function() { //whenever someone enters a route in this group, the trigger will run before the route runs.
    var route;
    if (!(Meteor.loggingIn() || Meteor.userId())) { // Checks if the user is logging in or if the user is logged in already
      route = FlowRouter.current();
      if (route.route.name !== 'login') {
        Session.set('redirectAfterLogin', route.path); // we don’t use the route name, but the path. this way you can redirect the user while keeping the state in the url.
        console.log("this is the route path", route.path); // we save the route that the user wanted to go in Session.set('redirectAfterLogin')
      }
      FlowRouter.go('login');
    }
  }]
});

loggedIn.route('/admin', {
  name: 'mainLayout',
  action: function() {
    BlazeLayout.render( 'mainLayout' );
  }
});

loggedIn.route('/pageOne', {
    action: function() {
      BlazeLayout.render( 'mainLayout', {content: 'pageOne'});
    },
    name: 'pageOne'
});

loggedIn.route('/pageTwo', {
    action: function() {
      BlazeLayout.render( 'mainLayout', {content: 'pageTwo'});
    },
    name: 'pageTwo'
});

loggedIn.route('/logout', {
    name: 'logout',
    action: function () {
        Meteor.logout(function () {
            FlowRouter.go(FlowRouter.path('login'));
        });
    }
});

这是我在exposed.js 中公开的路线:

var exposed = FlowRouter.group ({
  name: exposed
});

exposed.route('/', {
    action: function() {
      BlazeLayout.render( 'landing' );
    },
    name: 'landing'
});

exposed.route('/login', {
    action: function() {
      BlazeLayout.render( 'login' );
    },
    name: 'login'
});

exposed.route('/register', {
    action: function() {
      BlazeLayout.render( 'register' );
    },
    name: 'register'
});

最后但同样重要的是,在我的启动文件夹中,我有一个名为 default.js 的文件:

FlowRouter.wait();

//if the roles subscription is ready, start routing
//there are specific cases that this reruns, so we also check
// that FlowRouter hasn't initalized already

Tracker.autorun(function() {
  if (Roles.subscription.ready() && !FlowRouter._initialized) {
    return FlowRouter.initialize();
  }
});
// Run this when the meteor app is started
Meteor.startup(function () {

});

有没有其他人遇到过这个问题或类似问题?任何帮助将不胜感激。澄清一下,Sessions 已安装,我使用的是最新版本 1.1.6。谢谢

【问题讨论】:

  • 问题解决了吗?如果没有,目前的状态如何?

标签: meteor meteor-accounts flow-router


【解决方案1】:

有两个不同的问题:

  1. Session 只在客户端定义,在服务器上运行重定向代码是没有用的(在服务器上,钩子通常用于不同的目的,例如分析)。这会导致您在控制台中看到的错误。

  2. 您将undefined 传递给FlowRouter.go(),因为您将返回的会话值与null 进行严格比较。 nullundefined 不一样。因此,如果 'redirectAfterLogin' 会话变量未设置,您将获得 undefined 并将其传递给 FlowRouter。

【讨论】:

    【解决方案2】:

    遇到了类似的问题。通过以下方式解决了它: 在 AccountsTemplates.configure 中,我设置了 homeRoutePath: '/loggedin' 路径。这样,登录后,用户将被重定向到“/loggedin”路由。 然后在 FlowRouter 中为“/loggedin”路径:

    loggedInGroup.route('/loggedin', {
        name: 'loggedin',
        triggersEnter: [function(context, redirect) {
            var redirectAfterLoginPath = Session.get('redirectAfterLogin');
            if(redirectAfterLoginPath){
                redirect(redirectAfterLoginPath);
                Session.set('redirectAfterLogin', undefined);
            } else {
                redirect('home');
            };
        }]
    });
    
    loggedInGroup = FlowRouter.group({
        triggersEnter: [
            function(context, redirect, stop) {
                if (Meteor.loggingIn() || Meteor.userId()) {
                    route = FlowRouter.current();
                } else {
                    if(context.path != '/sign-in'){
                        Session.set('redirectAfterLogin', context.path);
                    };
                    redirect('/sign-in');
                }
            }
        ]
    });
    

    这种方式'/loggedin'路由只是处理无法访问Accounts.onLogin中的Session问题的方法,它将用户重定向到正确的路由。

    【讨论】:

      猜你喜欢
      • 2016-07-21
      • 2017-11-21
      • 2014-05-08
      • 2016-07-27
      • 2016-05-15
      • 1970-01-01
      • 2016-07-29
      • 2015-11-05
      • 1970-01-01
      相关资源
      最近更新 更多