简体   繁体   English

EmberJS嵌套路线模型

[英]EmberJS nested route model

I'm using EmberJS 1.13.3, EmberData 1.13.4, active-model-adapter 1.13.3 and I'm trying to load some model data from the store. 我正在使用EmberJS 1.13.3,EmberData 1.13.4,活动模型适配器1.13.3,我正在尝试从商店加载一些模型数据。 The parent route makes the call to the API and works as expected, but the child route calls the API but returns with the following error: 父路由调用API并按预期工作,但子路由调用API但返回时出现以下错误:

Cannot read property '_internalModel' of undefined TypeError: Cannot read property '_internalModel' of undefined

My code looks like this: 我的代码看起来像这样:

File: routes/user.js 文件: routes/user.js

import Ember from 'ember';

export default Ember.Route.extend({
  titleToken: function() {
    return 'Profile';
  },

  model: function(params) {
    return Ember.RSVP.hash({
      user: this.store.findRecord('user', params.username)
    });
  },

  setupController: function(controller, models) {
    this._super(controller, models);
    controller.setProperties(models);
  }
});

File: routes/user/posts.js 文件: routes/user/posts.js

import Ember from 'ember';

export default Ember.Route.extend({
  titleToken: function() {
    return 'Posts';
  },

  model: function() {
    return Ember.RSVP.hash({
      posts: this.store.findRecord('post', 'my_username')
    });
  },

  setupController: function(controller, models) {
    controller.setProperties(models);
  }
});

The JSON response for both looks the following: 两者的JSON响应如下所示:

User route: 用户路线:

{
  "user": {
    "id":"1",
    "username":"my_username",
    "first_name":"John",
    "last_name":"Doe",
    "post_ids":[1, 2]
  }
}

Posts route: 帖子路线:

{
  "posts":[
    { "id": 1, "author": "John Doe", "text": "My post", "username": "my_username", "user_id": 1 },
    { "id": 2, "author": "John Doe", "text": "My post2", "username": "my_username", "user_id": 1 }
  ]
}

And the code for router.js router.js的代码

Router.map(function() {

  this.route('user', { path: '/:username' }, function() {
    this.route('posts');
  });

  this.route('login');
});

This error occurs when you pass something other than an ID to find or findRecord methods. 传递ID以外的其他内容以findfind findRecord方法时会发生此错误。 Not a very helpful error message I must say. 我必须说,这不是一个非常有用的错误消息。

Anyway you can read some documentation on this here: http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_simplified-find-methods 无论如何,你可以在这里阅读一些文档: http//emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_simplified-find-methods

I had the same error. 我有同样的错误。 In my case the problem was that my fixtures were inconsistent, as shown in the example below: 在我的情况下,问题是我的灯具不一致,如下例所示:

GET /sports

[{id: 1, name: 'Sport A'}, {id:2, name: 'Sport B'}]

GET /sports/1
{id: 1, name: 'Sport C'}

I had the same error, and I managed to isolate the correct line of code causing the problem by looking at the "Promises" tab in Ember Inspector, searching only in rejecting promises, and one of them had the fulfillment / rejection value "TypeError: Cannot read property '_internalModel' of undefined". 我有同样的错误,我设法通过查看Ember Inspector中的“Promises”选项卡来隔离导致问题的正确代码行,仅搜索拒绝承诺,其中一个具有履行/拒绝值“TypeError:无法读取未定义的属性'_internalModel'。 The label of the promise gave me the operation, model and id of the record failing. 承诺的标签给了我记录失败的操作,模型和id。 https://github.com/emberjs/ember.js/issues/11863#issuecomment-123898242 https://github.com/emberjs/ember.js/issues/11863#issuecomment-123898242

In my case, I was returning {} from the server as a response to a findRecord method. 就我而言,我从服务器返回{}作为对findRecord方法的响应。

In my case the server wasn't returning the root element. 在我的情况下,服务器没有返回根元素。 So, for an user server was returning: 因此,对于用户服务器返回:

{name: name, surname: surname}

instead 代替

user: {name: name, surname: surname}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM