Ember模型挂钩,数据以REST形式接收为JSON

时间:2016-09-08 17:14:44

标签: javascript json django rest ember.js

我真的很喜欢ember,而且我正在尝试使用我的REST后端发送JSON数据(我使用Django REST框架和Django JSON API),但是我是得到一些奇怪的错误。

FIREFOX

错误:

Error while processing route: sampledata.index data is null _pushInternalModel@http://localhost:4200/assets/vendor.js:76923:11 push@http://localhost:4200/assets/vendor.js:76900:31

和警告:

WARNING: Encountered a resource object with type "sampledata", but no model was found for model name "sampledatum" (resolved model name using 'frontend@serializer:application:.modelNameFromPayloadKey("sampledata")).

CHROME

错误:

Error while processing route: sampledata.index Cannot read property 'type' of null TypeError: Cannot read property 'type' of null
at Class._pushInternalModel (http://localhost:4200/assets/vendor.js:76923:27)
at Class.push (http://localhost:4200/assets/vendor.js:76900:36)
at http://localhost:4200/assets/vendor.js:77627:15
at Object.run (http://localhost:4200/assets/vendor.js:10805:25)
at Class._adapterRun (http://localhost:4200/assets/vendor.js:77149:31)
at http://localhost:4200/assets/vendor.js:77624:13
at tryCatch (http://localhost:4200/assets/vendor.js:63933:14)
at invokeCallback (http://localhost:4200/assets/vendor.js:63948:15)
at publish (http://localhost:4200/assets/vendor.js:63916:9)
at http://localhost:4200/assets/vendor.js:42181:7logError @ ember.debug.js:28535error @ ember.debug.js:28478triggerEvent @ ember.debug.js:28594trigger @ ember.debug.js:53473trigger @ ember.debug.js:53287(anonymous function) @ ember.debug.js:53107tryCatch @ ember.debug.js:53806invokeCallback @ ember.debug.js:53821publish @ ember.debug.js:53789publishRejection @ ember.debug.js:53724(anonymous function) @ ember.debug.js:32054invoke @ ember.debug.js:333flush @ ember.debug.js:397flush @ ember.debug.js:205end @ ember.debug.js:560run @ ember.debug.js:682join @ ember.debug.js:702run.join @ ember.debug.js:21181hash.success @ rest.js:910fire @ jquery.js:3187fireWith @ jquery.js:3317done @ jquery.js:8757(anonymous function) @ jquery.js:9123
other error.. TypeError: Cannot read property 'type' of null

我发送的数据是:

{
    "links": {
        "first": "http://localhost:8000/api/sampledata?page=1",
        "last": "http://localhost:8000/api/sampledata?page=1",
        "next": null,
        "prev": null
    },
    "data": [{
        "type": "sampledata",
        "id": "4",
        "attributes": {
            "blind_id": "1-146788",
            "aliquots_circulation": 9,
            "total_dna": 120,
            "data_type": "WES"
        },
        "relationships": {
            "projects": {
                "data": [],
                "meta": {
                    "count": 0
                }
            }
        }
    }],
    "meta": {
        "pagination": {
            "page": 1,
            "pages": 1,
            "count": 1
        }
    }
}

我的余烬模型(/app/models/sampledata.js):

import DS from 'ember-data';

export default DS.Model.extend({
  blind_ID: DS.attr('string'),
  aliquotsCirculation: DS.attr('number'),
  totalDna: DS.attr('number'),
  dataType: DS.attr('string'),
  projects: DS.hasMany('project')
});

我的适配器(/app/adapters/application.js):

import DS from 'ember-data';
import ENV from 'frontend/config/environment';

export default DS.JSONAPIAdapter.extend({
  host: ENV.host,
  namespace: 'api'
});

我的序列化程序(/app/serializers/application.js)

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({

});

如果我将序列化程序更改为

export default DS.RESTSerializer.extend({
  primaryKey: '_id',
  serializeId: function(id) {
    return id.toString();
  }
});

我得到以下警告且没有错误,但没有显示数据:

WARNING: Encountered "links" in payload, but no model was found for model name "link" (resolved model name using frontend@serializer:application:.modelNameFromPayloadKey("links"))
vendor.js (line 16826)
WARNING: Encountered "data" in payload, but no model was found for model name "datum" (resolved model name using frontend@serializer:application:.modelNameFromPayloadKey("data"))

2 个答案:

答案 0 :(得分:2)

这是一个错字。您扩展适配器而不是序列化器。

//(/app/serializers/application.js)

    import DS from 'ember-data';

    export default DS.JSONAPISerializer.extend({

    });

解决警告

WARNING: Encountered a resource object with type "sampledata", but no model was found for model name "sampledatum" (resolved model name using 'frontend@serializer:application:.modelNameFromPayloadKey("sampledata")).

在这样的序列化器中覆盖:

modelNameFromPayloadType(type){
  if(type === 'sampledata') return type;
  return this._super(...arguments);
}

答案 1 :(得分:1)

最后几个小时后我才发现它。

Ember有一个名为Inflector的东西,它将我的单词“data”多元化为“datum”。这就是为什么它没有找到我的模型,这就是为什么将我的模型更改为sample-data.js的原因不起作用。

参考此处:https://guides.emberjs.com/v2.0.0/models/customizing-adapters/#toc_pluralization-customization

两种解决方案:

1)重命名模型和模板。模型应该是单数,模板为复数

2)使用Ember.Inflector (/app.js)

import Ember from 'ember';
export function initialize(/* container, application */) {
  var inflector = Ember.Inflector.inflector;
  inflector.uncountable('sampledata');
}

export default {
  name: 'inflector',
  initialize: initialize
};