Search code examples
javascriptnode.jsmeteoriron-router

Iron-Router: onBeforeAction() -> .next() is not a function


i got a problem with my meteor app and i don't know why.

My meteor version is 1.1.0.3 and here is a list of my packages:

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 ...

Alright... now we talk about my problem. i would like to protect some routes for users who don't have the "admin" role, that works find. The System checks my role right, but they don't render the view.

Error msg in console

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'] );
}

thanks for your answers!


Solution

  • You can directly store the this.next function in your me variable and call it as such:

    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');
                }
            });
        }
    },