Search code examples
meteormeteor-blazeflow-router

MeteorJS, Flow Router - Uncaught TypeError: pathDef.replace is not a function


I'm getting Uncaught TypeError: pathDef.replace is not a function console error using Flow Router in MeteorJS. I'm new to Flow having used Iron Router before so probably not doing something correctly.

Note that it works fine if I load another page first and then navigate to this page but I get the error if I reload the page.

Below is the faulty code:

Client template

{{#if Template.subscriptionsReady}}
  {{#each users}}
    <tr>
        <td>
            {{linkNames profile.firstname profile.lastname}}
        </td>
        <td>
            {{username}}
        </td>
        <td>
            {{emails.[0].address}}
        </td>
        <td>
            {{toUpperCase roles.[0]}}
        </td>
        <td>
            {{getUsernameById createdBy}}
        </td>
        <td>
            <a href="#" class="text-primary admin-edit-user" data-toggle="modal" data-target="#editUser" id="{{_id}}"><i class="fa fa-edit"></i></a>
        </td>
        <td>
            <a href="#" class="text-danger admin-delete-user" id="delete{{_id}}"><i class="fa fa-times"></i></a>
        </td>
    </tr>
    {{else}}
    <tr>
        <td colspan="6">
            <p>There are no users</p>
        </td>
    </tr>
    {{/each}}
    {{else}}
        <p>Loading...</p>
    {{/if}}

Pub

/* Users */

Meteor.publish('users', function() {
if (Roles.userIsInRole(this.userId, ['admin', 'team'])) {
    return Meteor.users.find({}, {
        fields: {
            'profile.firstname': 1,
            'profile.lastname': 1,
            'emails': 1,
            'username': 1,
            'roles': 1,
            'createdBy': 1
        },
        sort: {'roles': 1}
    })
} else if (Roles.userIsInRole(this.userId, ['client'])) {
    return Meteor.users.find({}, {
        fields: {
            'profile.firstname': 1,
            'profile.lastname': 1,
            'emails': 1,
            'username': 1
        }
    });
}
});

Client JS

/* On created */

Template.users.onCreated(function() {
var instance = this;

instance.autorun(function() {
    instance.users = function() {
        instance.subscribe(Meteor.users.find({}));
    }
});
});

/* Helpers */

Template.users.helpers({
users: function() {
    var users = Meteor.users.find({});
    return users;
}
});

I also get an error Exception in template helper: TypeError: Cannot read property 'username' of undefined in other templates for the following global helper (although the helper works as expected):

/* Current Username */

Template.registerHelper('currentUsername', function() {
    return Meteor.user().username;
});

Solution

  • Your first error is probably happening due to an error in your routing code. Make sure you've defined the parameters in the route and are using them in any routing code correctly.

    The second error is because Meteor.user() is not guaranteed to always be defined immediately. Change your helper to:

    Template.registerHelper('currentUsername', function() {
        var user = Meteor.user()
        if( user ) {
          return username;
        }
    });