【问题标题】:Ember model hooking with data received as JSON with RESTEmber 模型与以 REST 形式接收的 JSON 数据挂钩
【发布时间】:2017-01-16 17:31:34
【问题描述】:

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

火狐

错误:

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")).

错误:

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"))

【问题讨论】:

  • 您确定您在回复中没有输入sampledatum 而不是sampledata
  • 是的。无论如何,我检查了代码并重新检查。

标签: javascript json django rest ember.js


【解决方案1】:

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

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

【讨论】:

  • 当我修复我在此处发布之前给出的错字时,它会给出其他错误。我已经玩了一段时间了......我在某些时候感到困惑。我更新了错误。它会在错误中给出警告
  • 警告仍然出现(和错误)。 Google chrome 调试器对错误说一些更有用的信息:ember.debug.js:28535 处理路由时出错:sampledata.index 无法读取 null 的属性“类型”TypeError:无法读取 null 的属性“类型”
  • @MarcusRenno 我认为这与您选择的型号名称有关。我的意思是sampledatasampledata.js 模型文件。请将文件名更改为 sample-data.js 并在该函数 (modelNameFromPayloadType) 中返回“SampleData”
  • 把models/sampledata.js改成models/sample-data.js?我改变了它,现在它抱怨它找不到模型样本数据。 “处理路由时出错:sampledata.index 没有找到'sampledata'的模型”
  • @MarcusRenno 是的。我认为您需要遵循正确的命名。 Ember 是传统的。更改模型名称后,您还需要更正路线名称。同样在modelNameFromPayloadType 中,您应该返回正确的模型名称和新的模型名称
【解决方案2】:

我终于在几个小时后弄明白了。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多