Search code examples
jsonember.jsjson-api

normalizeResponse must return a valid JSON API document:


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.)


Solution

  • 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)
        }
    }