Ember 路线:无法读取 属性 未定义的“id”

Ember Route: Cannot read property ‘id’ of undefined

我收到此错误,我正在尝试检索一个故意不存在的用户:

Error while processing route: user Cannot read property 'id' of undefined TypeError: Cannot read property 'id' of undefined

我可以通过路由或控制器向用户显示一条错误消息 'the user does not exist' 或类似的消息,而不是此控制台错误吗?

这是我的路线

App.Router.map(function () {
    this.resource('user', { path: '/user/:user_id' });
});

我在这里检索用户

App.UserRoute = Ember.Route.extend({
    model: function(params) {
        return this.store.find('user', params.user_id);
    },
});

希望您能理解并帮助我继续。谢谢您的问候!

所以我不确定为什么缺少固定装置数据会给您带来特定的错误,但是您可以使用 error 事件捕获路由中的错误。你可以阅读它 here。你必须做这样的事情:

App.UserRoute = Ember.Route.extend({
    model: function(params) {
        return this.store.find('user', params.user_id);
    },
    actions: {
        error: function(error, transition) {
            // Display some sort of message
            alert("Sorry, we couldn't find that user.");
            // Redirect to a different part of the application
            this.transitionTo('index');
        }
    }
});