2016-05-20 18 views
0

ここにあるドキュメントに基づいて:https://guides.emberjs.com/v2.5.0/models/customizing-serializers/#toc_jsonserializer JSONSerializerは私のAPIに使用するための正しいシリアライザです。JSONSerializer EmberJSが予想される形式を受け入れていません

私は、次のエラーが表示されているにもかかわらず:

ember.debug.js:28535 Error while processing route: core.admin.users Cannot read property 'type' of undefined TypeError: Cannot read property 'type' of undefined

APIの応答は次のようになります。

import Model from 'ember-data/model'; 
import attr from 'ember-data/attr'; 

export default Model.extend({ 
    first_name: attr('string'), 
    last_name: attr('string') 

}); 

マイアプリケーションアダプタ:

{ 
    "errors": [], 
    "data": [ 
     { 
      "id": 1, 
      "first_name": "Foo", 
      "last_name": "Bar" 
     }, 
     { 
      "id": 2, 
      "first_name": "Moo", 
      "last_name": "Boo" 
     } 
    ], 
    "version": 1 
} 

私のモデルはそうのようになります。

import RESTAdapter from 'ember-data/adapters/rest'; 

export default RESTAdapter.extend({ 
    headers: { 
     'Accepts': 'application/json', 
     'Accept': 'application/json' 
    } 
}); 

マイユーザーアダプタ:

import ApplicationAdapter from './application'; 

export default ApplicationAdapter.extend({ 
    buildURL: function(){ 
     return 'core/users/foobar'; 
    } 
}); 

そして最後に私のアプリケーションシリアライザ:

import JSONSerializer from 'ember-data/serializers/json'; 

export default JSONSerializer.extend({ 
}); 

答えて

0

JSONSerializerはレスポンスのルート要素を期待していないので、これは動作しません。

自分で拡張する必要があります。多分このように:

normalizeResponse (store, primaryModelClass, payload, id, requestType) { 
    this._super(store, primaryModelClass, payload.data, id, requestType) 
} 
+0

あなたは完全にラックスです。私は自分のオブジェクトからID属性も忘れていました。上記の質問を修正します – valanto

関連する問題