Reputation: 11
Here is my Code:
Inside model:
import Model, { attr } from '@ember-data/model';
export default class TodoModel extends Model {
@attr('number') userId;
@attr('string') title;
@attr('boolean') completed;
}
Inside Adapter:
import RESTAdapter from '@ember-data/adapter/rest';
export default class TodoAdapter extends RESTAdapter {
host = 'https://jsonplaceholder.typicode.com';
}
Inside Serializer:
import RESTSerializer from '@ember-data/serializer/rest';
export default class TodoSerializer extends RESTSerializer {
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
payload = {
"todos":payload
}
return this._super(...arguments);
}
}
Inside Route:
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class HelloRoute extends Route {
@service store;
model() {
return this.store.findRecord('todo',1);
}
}
Error:
Error while processing route: hello Assertion Failed: normalizeResponse must return a valid JSON API document:
Top level of a JSON API document must be an object Error: Assertion Failed: normalizeResponse must return a valid JSON API document:
Top level of a JSON API document must be an object
(My point of View: The Error is coming while processing the Serializer, how to return a JSON API document from normalizeResponse function.)
Upvotes: 0
Views: 679
Reputation: 11
The below code will work fine.
import RESTSerializer from '@ember-data/serializer/rest';
export default class TodoSerializer extends RESTSerializer {
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
return this.normalize(primaryModelClass,payload)
}
}
Upvotes: 1
Reputation: 5955
Looks like you're definitely on the right track. I would suggest capturing the output of your call in normalizeResponse to a new variable so you can inspect it:
import RESTSerializer from '@ember-data/serializer/rest';
export default class TodoSerializer extends RESTSerializer {
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
payload = {
"todos":payload
}
let returnPayload = this._super(...arguments);
console.log(returnPayload);
return returnPayload;
}
}
That will give you a much better sense of what is going on. The output there needs to be in JSON:API format (see https://jsonapi.org for examples) so you can either delegate that to the RESTSerializer parent class or just do the appropriate transformations of your payload yourself (which will end up being faster from a performance point of view).
Upvotes: 2