Search code examples
meteoriron-router

Meteor Router with Meteor call


I having trouble to validate a user with a Meteor.call on Router. Only return "undefined" pr

"Exception in delivering result of invoking 'verifyUserRole': TypeError: undefined is not a function"

Router.route("/admin", function(){
    Meteor.call("verifyUserRole", function(error, result){
        if(result){
            this.render('adminDashboard');
            this.layout("adminLayout");
        } else {
            this.render('adminLogin');
        }
    })
});

 Meteor.methods({
   "verifyUserRole" : function(){
       if(this.userId){
       var user = Meteor.user();
       var role = user.profile.role;
       if(role == "admin"){
           return true;
       } else {
           Session.set("adminLoginError", "Restrict Area");
           return false;
       }
   }
}});

Solution

  • The value of this is referring to your inner function instead of your outer function. Try the following:

    Router.route("/admin", function(){
      var self = this;
      Meteor.call("verifyUserRole", function(error, result){
        if(result){
          self.render('adminDashboard');
          self.layout("adminLayout");
        } else {
          self.render('adminLogin');
        }
      });
    });