2015-02-24 55 views
1

我有這個模型,當使用此路由器和模型導航到此路由時會引發錯誤。這是導致問題的註冊表。Ember數據錯誤:型號:@ each`,必須是「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) 

我不知道是否有我的模擬呢?

apprev:

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

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

    ); 
    }); 
+0

你介意發佈錯誤的完整堆棧跟蹤?也請告訴我們您正在使用的Ember和Ember-Data的版本。 – GJK 2015-02-24 14:01:24

+0

@GJK加了謝謝 – SuperUberDuper 2015-02-24 14:43:19

+0

@GJK也許是因爲我的路線模型是空的,我沒有在路線上填充它 – SuperUberDuper 2015-02-24 14:44:37

回答

2

你的API需要返回以某種方式格式化的JSON數據。 Ember需要一個與模型名稱一致的對象。所以像這樣:

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

如果你無法控制從API終端返回,寫DS.RESTSerializer到JSON轉成一些灰燼數據就是了。例如,如果你的服務器負載看起來是這樣的:

[{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格式灰燼數據預計, 。

相關問題