简体   繁体   English

我正在使用iron:router v1.0.7,但是this.render()是“ undefined”。 我看不到我在做什么

[英]I'm using iron:router v1.0.7 but this.render() is “undefined”. I can't see what I'm doing wrong

I'm displaying an access denied template when the visitor tries to visit the Submit page. 访问者尝试访问“提交”页面时,我正在显示一个拒绝访问模板。 This works fine. 这很好。 Here is the code: 这是代码:

Router.route('/', {name: 'etoeventsList'});
Router.route('/submit', {name: 'etoeventSubmit'});

var requireLogin = function () {
  if (!Meteor.user()) {
    this.render('accessDenied');
  } else {
    this.next();
  }
};

Router.onBeforeAction(requireLogin, {only: 'etoeventSubmit'});

I want to utilize "requireLogin" under a different context (anonymous user visiting '/') so I thought I would add an argument to allow me to pass in the template to be rendered. 我想在不同的上下文(匿名用户访问“ /”)下使用“ requireLogin”,所以我想我要添加一个参数以允许我传递要渲染的模板。 Like this: 像这样:

var requireLogin = function (template) { // now with argument 'template'
  if (!Meteor.user()) {
    this.render(template); // using 'template'
  } else {
    this.next();
  }
};

Router.onBeforeAction(requireLogin('accessDenied'), {only: 'etoeventSubmit'}); // passing 'accessDenied'
Router.onBeforeAction(requireLogin('index'), {only: 'etoeventsList'}); // passing 'index'

The error I receive is Uncaught TypeError: undefined is not a function and the template I want to show does not display. 我收到的错误是Uncaught TypeError: undefined is not a function并且我要显示的模板没有显示。

You can't do it that way. 你不能那样做。 The way you are doing it, this will refer to Window . 你正在做的方式, 将参考窗口 You will have to use an anonymous function as a param to onBeforeAction . 您将必须使用匿名函数作为onBeforeAction的参数。

Router.configure({
    layoutTemplate: 'layout',
    loadingTemplate: 'loading',
    notFoundTemplate: 'notFound',
    waitOn: function() {

    }
});

if(Meteor.isClient) {
  Router.onBeforeAction(function() {
    // is user logged in
    if (! Meteor.user()) {
      if (Meteor.loggingIn()) {
        this.render('loading');
      } else {
        this.render('accessDenied');
      }
    } else {
      this.next();
    }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码出现故障,但我看不出我做错了什么 - My code is malfunctioning, but I can't see what I'm doing wrong 我需要使用循环找出数组的每个字符串中有多少个元音字母。 看不出我做错了什么 - I need to find how many vowels are in each string of the array using loops. Can't see what I'm doing wrong 将JS代码转换为React.js,看不到我在做什么 - Converting JS code to React.js, can't see what I'm doing wrong jQuery命令未执行,看不到我在做什么 - Jquery command not executing, can't see what I'm doing wrong 我无法使用jQuery,也不确定自己在做什么错 - I can't get my jQuery to work and I'm not sure what I'm doing wrong 我正在尝试使用解构赋值提取 object 的值,年龄变量表示未定义。 我做错了什么? - I'm trying to extract the values of an object using destructuring assignment, the age variable says undefined. What I'm doing wrong? AngularJS-我在注射中做错什么? - Angularjs - what I'm doing wrong with injections? 我不知道我在使用 getDay() 时做错了什么 - I can't figure out what I'm doing wrong with getDay() 谁能告诉我我做错了什么 - Can anybody tell me what I'm doing wrong 循环执行不正确,无法弄清楚我在做什么 - Loop implemented incorrectly, can't figure out what I'm doing wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM