Ember.js破坏记录时出错

Sun*_*tva 0 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.
Run Code Online (Sandbox Code Playgroud)

我的REST API返回一个200带有空对象响应的状态代码{}.我认为这就是问题所在,所以我一直在尝试自定义几个序列化程序挂钩(normalizeDeleteRecordResponse,extractDeleteRecord甚至只是normalizeResponse),但实际上并没有调用它们.

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

Kar*_*ren 5

Ember Data的默认适配器遵循JSON API规范,因此在删除记录(或在规范中调用的资源)时,您应该返回204 No Content响应(没有内容)或200 OKif返回其他元数据(必须在节点中)命名meta).只返回一个空对象200 OK在规范中无效,你最好的解决办法就是修复你的rest api以遵循规范.

现在,如果这完全不可能,您可以通过创建基于的自定义适配器JSONAPIAdapter然后覆盖来解决此问题deleteRecord.可能是基于默认实现的类似内容:

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;
    });
}
Run Code Online (Sandbox Code Playgroud)