将JSON响应导入模型错误Ember-cli

时间:2014-08-27 15:15:29

标签: ember.js ember-data ember-cli ember-router

这是代码app / routes / profile.js

import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin, {
    model: function() {
        var _this = this;
        return Ember.$.get('/user/profile').then(function(data) 
            _this.store.push('profile', data);
        });
    }
});

应用程序/模型/ profile.js

import DS from "ember-data";


export default DS.Model.extend({
    name:   DS.belongsTo('name')

});

应用程序/模型/ name.js

import DS from "ember-data";


export default DS.Model.extend({
    firstName:   DS.attr('string'),
    lastName:   DS.attr('string')
});

应用程序/串行器/ profile.js

import DS from "ember-data";

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        name: {embedded: 'always'}
    }
});

我收到错误无法读取属性' typeKey'未定义的

这是来自服务器的JSON响应。

{
    "id":"1",
    "name":{
        "id":"1",
        "firstName":"first name",
        "lastName":"last name"
    }       
}

1 个答案:

答案 0 :(得分:1)

您需要服务器返回包装在类型对象中的有效负载:

{
    "profiles": [{
        "id":"1",
        "name":{
            "id":"1",
            "firstName":"first name",
            "lastName":"last name"
        }       
    }]
}