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

时间:2021-04-27 20:36:42

I'm getting this error, where I am trying to retrieve a user that DO NOT exist on purpose ofcouse:

我收到此错误,我试图检索一个不存在的用户:

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

处理路由时出错:user无法读取未定义的属性'id'TypeError:无法读取未定义的属性'id'

Instead of this console error, can I give the user an error message saying, 'the user does not exist' or something like that through the route or a controller to the view?

而不是这个控制台错误,我可以给用户一个错误消息,说“用户不存在”或类似的东西通过路由或控制器到视图?

Heres my route

继承人我的路线

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

And here i retrieve the user

在这里我检索用户

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

Hope you understand and can help me proceed. Thanks in regards!

希望你理解,可以帮助我继续。谢谢你!

1 个解决方案

#1


1  

So I'm not sure why missing fixtures data is giving you that particular error, but you can catch errors in routes using the error event. You can read about it here. You'll have to do something like this:

所以我不确定为什么缺少fixtures数据会给你那个特定的错误,但你可以使用error事件捕获路由中的错误。你可以在这里读到它。你必须做这样的事情:

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');
        }
    }
});

#1


1  

So I'm not sure why missing fixtures data is giving you that particular error, but you can catch errors in routes using the error event. You can read about it here. You'll have to do something like this:

所以我不确定为什么缺少fixtures数据会给你那个特定的错误,但你可以使用error事件捕获路由中的错误。你可以在这里读到它。你必须做这样的事情:

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');
        }
    }
});