Search code examples
javascriptmeteorflow-router

Root route is ignored when I add /admin route in FlowRouter


My home route works when it's by itself:

FlowRouter.route('/', {
  name: 'home',
  triggersEnter: [
    function(context, redirect) {
      var ExperimentsSub, handle, randomExperiment;
      console.log('Home triggers');
      ExperimentsSub = new SubsManager;
      handle = ExperimentsSub.subscribe('randomExperiment');
      if (handle.ready && Experiments.find.count) {
        randomExperiment = Experiments.findOne;
        return redirect('/experiment/' + randomExperiment._id);
      }
    }
  ],
  action: function() {
    console.log('Rendering home');
    return BlazeLayout.render('layout', {
      content: 'home'
    });
  }
});

But when I add in my /admin route, surfing to / routes through the admin route instead.

FlowRouter.route('/admin', {
  name: 'admin',
  triggersEnter: [console.log('Admin triggers'), !Roles.userIsInRole(this.userId, ['admin']) ? FlowRouter.go(FlowRouter.path('login')) : void 0],
  action: function() {
    console.log('Rendering admin');
    return BlazeLayout.render('layout', {
      content: 'admin'
    });
  }
});

I know this because of the console logging I'm doing. When I surf to / with both routes, the console output is Rendering admin. Why is it doing this, and how can I fix it?


Solution

  • The problem was in my admin route triggers. I was not assigning an array of functions. I'm not clear why this created the side effects I saw, but correcting it fixed the route.

    The route with the fixed triggers property looks like this:

    FlowRouter.route '/admin',
      name: 'admin'
      triggersEnter: [ (context, redirect) ->
        console.log 'Admin triggers'
        unless Roles.userIsInRole this.userId, ['admin']
          redirect FlowRouter.path('login')
      ]
      action: ->
        console.log 'Rendering admin'
        BlazeLayout.render 'layout', content: 'admin'