javascript - Ember 模型与使用 REST 作为 JSON 接收的数据 Hook

标签 javascript json django rest ember.js

我是 ember 的新手,我正在尝试使用我的 REST 后端发送 JSON 数据(我正在使用 Django REST 框架和 Django JSON API),但我遇到了一些奇怪的错误。

火狐

错误:

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
        }
    }
}

我的 Ember 模型(/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"))

最佳答案

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

//(/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);
}

关于javascript - Ember 模型与使用 REST 作为 JSON 接收的数据 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39396762/

相关文章:

javascript - 多次链接到同一页面的正确方法是什么?

PHP json_encode 转 JS

jquery - 从动态列表创建 JSON 对象

javascript - 将 AJAX json 数据传递给 D3

Css 更改未反射(reflect)在 Django 中

javascript - 如何从 HTML 元素更改特定的内联样式?

javascript - jQuery 'Slides' 无法运行

json - Cloudformation 错误(属性 Step adjustment 的值必须是对象列表)

python - Django/Python 代码 : readable vs fast code

django - 在 django 时间字段中将输入设置为 AM PM