Ember-Data "Error: Assertion Failed: You made a 'findRecord' request for a 'user' with id 'current', but the adapter's response did not have any data"

Ember-Data "Error: Assertion Failed: You made a 'findRecord' request for a 'user' with id 'current', but the adapter's response did not have any data"

我正在使用 Ember 3.17 并尝试设置 Ember 数据以使用 JSONAPIAdapter 进行服务器调用,但是我不断收到此错误:

Error: Assertion Failed: You made a 'findRecord' request for a 'user' with id 'current', but the adapter's response did not have any data

我的适配器有问题吗?

在下面找到我的适配器、路由和此调用的预期数据(我设置了一个模型,具有一个属性)。唯一有点奇怪的是我的端点是 /users/current 但当前与对象上的 ID 不匹配。 (但是,当我确实匹配了有效端点的 ID 时,我得到了同样的错误)

adapters/application.js

import JSONAPIAdapter from '@ember-data/adapter/json-api';
import ENV from 'rendia-tv-web/config/environment';

export default class ApplicationAdapter extends JSONAPIAdapter {
  host = ENV.APP.API_HOST;
  namespace = 'tv/v1';

  headers = {
    'Authorization': <authHeader>,
  };

  ajax = function(url, method, hash) {
    hash.crossDomain = true;
    hash.xhrFields = {withCredentials: true};
    return this._super(url, method, hash);
  }
  
}

routes/application.js

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class ApplicationRoute extends Route {

  @service store;

  async model() {
    return this.store.findRecord('user', 'current');
  }

}

预期数据

{
    "data": {
        "id": "12345",
        "type": "users",
        "attributes": {
            "username": "email@email.com",
            "name": "User 1"
        },
        "relationships": {
            "practice": {
                "data": {
                    "type": "practice",
                    "id": "55555"
                }
            }
        }
    },
    "included": [
        {
            "type": "practice",
            "id": "55555",
            "attributes": {
                "date_created": "2016-09-23T04:21:38-04:00",
                "name": "Practice Name",
                "expiration_date": "2024-10-23T23:59:59-04:00"
            }
        }
    ]
}

感谢任何帮助,谢谢!

据我所知 Ember 数据期望 API returns 一条与用于查找记录的 ID 相同的记录。如果您正在执行 store.findRecord('user', 'current'),它期望 API returns 具有 ID 'current' 的资源。它不支持您使用的别名。相反,它会抛出您的帖子中提到的断言。

据我所知,这 不是 JSON:API 规范的限制。该规范与 URL 结构无关。它只设置了一些高级规则:

Fetching Resources

A server MUST support fetching resource data for every URL provided as:

  • a self link as part of the top-level links object
  • a self link as part of a resource-level links object
  • a related link as part of a relationship-level links object

https://jsonapi.org/format/#fetching-resources

据我所知,它也不会阻止服务器从其他 URL 返回资源数据。

规范包含关于 URLs 更新资源的要求,您应该注意:

Updating Resources

A resource can be updated by sending a PATCH request to the URL that represents the resource.

The URL for a resource can be obtained in the self link of the resource object. Alternatively, when a GET request returns a single resource object as primary data, the same request URL can be used for updates.

https://jsonapi.org/format/#crud-updating

删除资源也是如此:

Deleting Resources

An individual resource can be deleted by making a DELETE request to the resource’s URL:

https://jsonapi.org/format/#crud-deleting

如果您不想也支持通过别名创建和删除资源,您必须包括资源的self link。

为了解决 Ember 数据的限制,我建议做一个很好的旧 fetch 请求并将结果推送到 Ember 数据的存储中手动使用store.pushPayload() API.