javascript - Iron-Router: onBeforeAction() -> .next() 不是函数

标签 javascript node.js meteor iron-router

我的 meteor 应用程序出现问题,我不知道为什么。

我的 meteor 版本是 1.1.0.3,这是我的软件包列表:

accounts-password        1.1.1  Password support for accounts
alanning:roles           1.2.13  Role-based authorization
chrismbeckett:toastr     2.1.2_1  Gnome / Growl type non-blocking notifications
coffeescript             1.0.6  Javascript dialect with fewer braces and semi...
email                    1.0.6  Send email messages
fortawesome:fontawesome  4.4.0  Font Awesome (official): 500+ scalable vector...
fourseven:scss           3.2.0  Style with attitude. Sass and SCSS support fo...
insecure                 1.0.3  Allow all database writes by default
iron:router              1.0.9  Routing specifically designed for Meteor
jquery                   1.11.3_2  Manipulate the DOM using CSS selectors
meteor-platform          1.2.2  Include a standard set of Meteor packages in ...

好吧...现在我们来谈谈我的问题。我想为没有“管理员” Angular 色的用户保护一些路由,这很有效。系统检查我的 Angular 色是否正确,但他们不呈现 View 。

控制台中的错误消息

Exception in delivering result of invoking 'accounts/hasAdminRole': TypeError: me.next is not a function
    at http://localhost:3000/lib/controllers/admin_controller.js?843e8c9edbf0891b773aa63a9ad004d1afcbfb19:28:9
    at Meteor.bindEnvironment [as _callback] (http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:983:22)
    at _.extend._maybeInvokeCallback (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:3860:12)
    at _.extend.receiveResult (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:3880:10)
    at _.extend._livedata_result (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4970:9)
    at onMessage (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:3725:12)
    at http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:2717:11
    at Array.forEach (native)
    at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:156:11)
    at _.extend._launchConnection.self.socket.onmessage (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:2716:11)

app/lib/controllers/admin_controller.js

onBeforeAction: function () {
    var me = this;
    if(!Meteor.userId()) {
        Router.go('signin');
    } else {
        Meteor.call('accounts/hasAdminRole', function(err, r) {
            if(!err && r) {
                console.log('success');
                console.log(me);
                me.next()
            } else {
                toastr.error('Not Authorized.', 'Error!');
                Router.go('home');
            }
        });
    }
},

app/server/methods.js

'accounts/hasAdminRole': function() {
    return Roles.userIsInRole( Meteor.user() , ['admin'] );
}

感谢您的回答!

最佳答案

您可以直接将 this.next 函数存储在您的 me 变量中并这样调用它:

onBeforeAction: function () {
    var me = this.next;
    if(!Meteor.userId()) {
        Router.go('signin');
    } else {
        Meteor.call('accounts/hasAdminRole', function(err, r) {
            if(!err && r) {
                console.log('success');
                me();
            } else {
                toastr.error('Not Authorized.', 'Error!');
                Router.go('home');
            }
        });
    }
},

关于javascript - Iron-Router: onBeforeAction() -> .next() 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32248506/

相关文章:

mongodb - 在不删除或覆盖现有字段的情况下更新 meteor 集合

javascript - 返回未定义的 Posts.findOne();

javascript - 是否可以制作一个按钮作为文件上传按钮?

javascript - 未检测到 Jquery 移动主题

javascript - 无法发送 PDF(*.pdf) 扩展名的文件

node.js - LoopBack 过滤器 "where and "在任何值中返回错误/相同结果

javascript - Angular等待服务函数Ajax完成

javascript - 如何在不使用window.location的情况下重定向到另一个html页面?

node.js - Grunt Imagemin 没有优化图像

meteor - 如何将 pdf.js 与 Meteor 一起使用?