Ember data error: model:@each`, must be of the form `type:name

Ember data error: model:@each`, must be of the form `type:name

我有这个模型,当我使用这个路由器和模型导航到这条路线时会抛出一个错误。这是导致问题的寄存器。

注册路由模型:

export default DS.Model.extend({
  newUser: DS.belongsTo('user'),
  password: DS.attr('string')
});

我为这个模型使用的注册路线:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function () {
    return this.store.find("account-type");
  }
});

这是我的路由器:

Router.map(function () {
  this.route('login');
  this.route('register');
  this.route('my-account');
  this.route('change-password');

  this.route('app', function () {
    this.resource('profile', {path: 'profile/profile_id'});
    this.route('connections');
    this.resource('conversation', {path: 'conversation/conversation_id'});
  });

  this.resource('myAccount', function() {});
  this.route('user');
});

用户模型:

export default DS.Model.extend({
  title: DS.attr('string'),
  firstName: DS.attr('string'),
  surname: DS.attr('string'),
  dateOfBirth: DS.attr('string'),
  telephoneNumber: DS.attr('string'),
  accountType: DS.belongsTo('accountType'),
  emailAddress: DS.attr('string'),

  //used to present full-name
  fullName: function () {
    return this.get('title') + ' ' +
      this.get('firstName') + ' ' +
      this.get('surname');
  }.property("title", "firstName", "surname")

});

我遇到的错误:

model:@each`, must be of the form `type:name

这是我得到的堆栈跟踪:

Error while processing route: register Invalid fullName: `model:@each`, must be of the form `type:name`  TypeError: Invalid fullName: `model:@each`, must be of the form `type:name` 
    at __exports__.default.EmberObject.extend.resolve (http://localhost:4200/assets/vendor.js:16812:17)
    at Object.resolve [as resolver] (http://localhost:4200/assets/vendor.js:16434:25)
    at resolve (http://localhost:4200/assets/vendor.js:14970:32)
    at Object.Container.resolve (http://localhost:4200/assets/vendor.js:14550:16)
    at factoryFor (http://localhost:4200/assets/vendor.js:15053:31)
    at Object.Container.lookupFactory (http://localhost:4200/assets/vendor.js:14657:16)
    at Ember.Object.extend.modelFactoryFor (http://localhost:4200/assets/vendor.js:73164:31)
    at ember$data$lib$serializers$json_serializer$$default.extend.extractArray (http://localhost:4200/assets/vendor.js:67267:22)
    at apply (http://localhost:4200/assets/vendor.js:32891:27)
    at superWrapper (http://localhost:4200/assets/vendor.js:32459:15)

不知道是不是和我的mock有关?

批准:

  accountTypesRouter.get('/', function (req, res) {
    res.send(
       ["foo", "boo"]

      /*
          [
       {"id": 1, "type": "foo"},
       {"id": 2, "type": "baa"}
       ]
      * */

    );
  });

您的 API 需要 return 以某种方式格式化 JSON 数据。 Ember 想要一个以模型名称为键的对象。所以像这样:

{
    accountTypes: [{
        id: 1, 
        type: 'foo'
    },{
        id: 2, 
        type: 'boo'
    }]
}

如果您无法控制从 API 端点 return 编辑的内容,请编写 DS.RESTSerializer 将 JSON 转换为 Ember 数据就是了。例如,如果您的服务器负载如下所示:

[{id:1, type:'foo'},{id:2, type:'boo'}]

你可以这样写一个序列化器:

import DS from 'ember-data';
export default DS.RESTSerializer.extend( {
    extractArray: function(store, type, payload) {
        payload = {
            'accountTypes': payload
        };
        return this._super(store, type, payload);       
    },
});

正如@Kori 指出的那样,DS.RESTAdapter docs 简要讨论了 JSON 格式 Ember 数据预期。