I'm using Ember 3.17 and trying to set up Ember Data to make a server call using the JSONAPIAdapter, however I keep getting this error:
Error: Assertion Failed: You made a 'findRecord' request for a 'user' with id 'current', but the adapter's response did not have any data
Am I doing something wrong with my adapter?
Below find my adapter, route and the expected data for this call (I have a model set up, with one attribute). The only thing that's a little weird is my endpoint is /users/current but current does not match the ID on the object. (However when I do match the ID, which is a valid endpoint, I get the same error)
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');
}
}
expected data
{
"data": {
"id": "12345",
"type": "users",
"attributes": {
"username": "[email protected]",
"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"
}
}
]
}
Any help is appreciated, thanks!
As far as I know Ember Data expects that the API returns a record with the same ID as used to find the record. If you are doing a store.findRecord('user', 'current')
it expect that the API returns a resource with the ID 'current'
. It does not support such aliases as you are using it. Instead it will throw the assertion mentioned in your posting.
As far as I know this is not a limitation of the JSON:API specification. The specification is agnostic about the URL structure. It only sets some high-level rules:
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
As far as I read that it does not prevent the server from returning resource data from other URLs as well.
The specification contains a requirement about URLs for updating a resource, which you should be aware of:
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.
The same is true for deleting a resource:
Deleting Resources
An individual resource can be deleted by making a DELETE request to the resource’s URL:
If you don't want to support creating and deleting resources through the alias as well you must include a self
link for the resource.
In order to work-a-round the limitation of Ember Data I would recommend to do a good old fetch
request and push the result into Ember Data's store manually using the store.pushPayload()
API.