【问题标题】:Ember cli mirage error: patch handler cannot read property update of nullEmber cli mirage 错误:补丁处理程序无法读取 null 的属性更新
【发布时间】:2018-05-29 09:27:08
【问题描述】:

我在我的 amber 应用程序中使用 ember cli mirage,我在夹具中定义了数据并使用 RestSerializer,我试图模拟更新记录的属性但收到错误:url api/survey 的补丁处理程序-groups/[id] 抛出错误:cannot read property update of null

mirage/config.js

 this.patch('/survey-groups/:id', function({ surveyGroups }, request) {
let id = request.params.id;
let attrs = this.normalizedRequestAttrs();

return surveyGroups.find(id).update(attrs);

});

mirage/serializers/application.js

import { RestSerializer } from 'ember-cli-mirage';
export default RestSerializer.extend({
primaryKey: 'keyId'});

app/serializers/application.js

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
primaryKey: 'keyId', });

夹具样本;海市蜃楼/fixtures/survey-groups.js

export default [
  {
    "code": "dfdj", 
    "description": "", 
    "keyId": 29116, 
  }, 
  {...... }]

我还注意到在服务器返回的数据中,每个记录都添加了一个 id 属性,带有一个字符串值,例如编号:“1” 当我尝试使用此字符串值代替 id 查找记录时,将返回记录。

可能导致此错误和行为的原因

【问题讨论】:

    标签: ember.js ember-cli-mirage


    【解决方案1】:

    ember-cli-mirage 的序列化程序没有primaryKey 选项。据我所知,海市蜃楼不提供任何自定义主键名称的可能性。因此你不能使用find 方法。我建议改用findByreturn surveyGroups.findBy({ keyId: id }).update(attrs);

    另一个选项是在有效负载的序列化 (serialize) 和规范化 (normalize) 上使用更改主键的名称。这种方法的好处是您仍然可以使用 mirage 的 shorthands

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      相关资源
      最近更新 更多