路由调度从未呈现.您是否忘记在onBeforeAction中调用this.next()

Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?

本文关键字:调用 onBeforeAction this next 忘记 调度 是否 路由      更新时间:2023-09-26

我现在正在Meteor中执行注销功能,我注意到了一些奇怪的事情。当我在登录页面上时,甚至在注销并重定向到登录页面时,我的 Google 控制台上会出现错误:

路由调度从未呈现。您是否忘记在onBeforeAction中调用this.next()?

这是我的代码:

router.js

var routerExcepts = [
  "login",
  "register"
];

Router.onBeforeAction(function() {
  if (Router.current().route.getName() != "adminlogin.:_id") {
    if (!Meteor.userId()) {
      return this.redirect("/login");
    } else {
      this.next();
    }
  }
}, {
  except: routerExcepts
});

home.html:

<template name="home">
  <div class="container">
     <a href="#" id="item-logout" class="btn btn-default btn-lg btn-block">Logout</a>
  </div>
</template>

home.js:

Template.home.events({
  "click #item-logout": function() {
    Meteor.logout();
  }
});

有人知道我的代码是怎么回事吗?我知道这与其他问题重复,我尝试了针对这些已回答问题提供的解决方案,但它不能解决我的问题。

提前谢谢。

您不需要将其设置为返回值,我认为这可能会导致您的问题,替换

return this.redirect("/login");

this.redirect("/login");

如果您仍然收到错误,请尝试将"this.redirect"替换为"Router.go"(如果我没记错的话,无论如何,这个.redirect代理都在引擎盖下),如下所示:

Router.go("/login");

你也可以删除 else 以确保始终触发 this.next():

Router.onBeforeAction(function() {
  if (Router.current().route.getName() != "adminlogin.:_id") {
    if (!Meteor.userId())
      Router.go("/login");
    this.next();
  }
}

尝试将this.next()置于条件之外:

Router.onBeforeAction(function() {
  if (Router.current().route.getName() != "adminlogin.:_id") {
    if (!Meteor.userId()) {
      return this.redirect("/login");
    }
  }
  this.next();
}