Ember.js破坏记录时出错

时间:2016-07-10 19:24:02

标签: ember.js ember-data

我正在尝试销毁记录,但我收到此错误

An adapter cannot assign a new id to a record that already has an id.
[…] had id: 25 and you tried to update it with null. This likely happened because
your server returned data in response to a find or update that had a different
id than the one you sent.

我的REST API返回一个200状态代码,其中包含空对象响应{}。我认为这就是问题所在,所以我一直在尝试自定义几个序列化程序挂钩(normalizeDeleteRecordResponseextractDeleteRecord,甚至只是normalizeResponse),但这些挂钩都不会被实际调用。

查看我的堆栈跟踪,错误似乎在didSaveRecord挂钩中,我假设它正在接收空的JSON有效负载并将其传递给updateId

2 个答案:

答案 0 :(得分:5)

Ember Data的默认适配器遵循JSON API规范,因此当deleting a record(或在规范中调用它们的资源)时,您应该返回204 No Content响应(没有内容)或如果返回其他元数据(必须位于名为200 OK的节点中),则为meta。只返回一个200 OK的空对象在规范中无效,您最好的解决方案是修复您的其余API以遵循规范。

现在,如果完全不可能,您可以通过创建基于JSONAPIAdapter的自定义适配器然后覆盖deleteRecord来解决此问题。可能是基于default implementation

的类似内容
deleteRecord(store, type, snapshot) {
  var id = snapshot.id;

  return this.ajax(this.buildURL(type.modelName, id, snapshot, 'deleteRecord'), "DELETE")
    .then(response => {
      if(Object.keys(response).length === 0) {
        return null; // Return null instead of an empty object, this won't trigger any serializers or trying to push new data to the store
      }
      return response;
    });
}

答案 1 :(得分:0)

delete的API应返回204状态代码