【问题标题】:MeteorJS, Flow Router - Uncaught TypeError: pathDef.replace is not a functionMeteorJS,流路由器 - 未捕获的类型错误:pathDef.replace 不是函数
【发布时间】:2016-03-22 10:20:42
【问题描述】:

我在 MeteorJS 中使用 Flow Router 时遇到 Uncaught TypeError: pathDef.replace is not a function 控制台错误。我是 Flow 的新手,之前使用过 Iron Router,所以可能做的不对。

请注意,如果我先加载另一个页面然后导航到此页面,它会正常工作,但如果我重新加载页面,则会收到错误。

下面是错误代码:

客户端模板

{{#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}}

酒馆

/* 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
        }
    });
}
});

客户端 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;
}
});

我还在以下全局帮助程序的其他模板中收到错误Exception in template helper: TypeError: Cannot read property 'username' of undefined(尽管帮助程序按预期工作):

/* Current Username */

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

【问题讨论】:

    标签: meteor meteor-blaze flow-router


    【解决方案1】:

    您的第一个错误可能是由于您的路由代码中的错误而发生的。确保您已经在路由中定义了参数,并且在任何路由代码中都正确使用了它们。

    第二个错误是因为 Meteor.user() 不能保证总是立即定义。将您的助手更改为:

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

    【讨论】:

    • 谢谢!我实际上确实知道了这一点,但忘了结束这个问题。我按照上面的方法做了 - 我的另一个问题是我在用户 sub 之前重定向到一个不存在的页面。奇怪的是,没有用户 pub/sub 一切正常 - 就像现在稍后定义的用户文档一样。
    猜你喜欢
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2019-06-06
    • 2019-05-24
    • 2021-12-15
    • 2019-10-26
    相关资源
    最近更新 更多