Problem with adapter and serializer

please help me i am in trouble with the following error in my ember application

Error while processing route: index Assertion Failed: You must include an ‘id’ for tasks in an object passed to ‘push’ Error: Assertion Failed: You must include an ‘id’ for tasks in an object passed to ‘push’

Uncaught (in promise) Error: Assertion Failed: You must include an ‘id’ for tasks in an object passed to ‘push’

iam unable to fine that where exact problem is occur.

Model File -

 import Model, { attr } from '@ember-data/model';
 export default class TasksModel extends Model {
   @attr('string') title;
   @attr('string') priyority;
   @attr('date') createdAt;
 }

Adapter file -

import RESTAdapter from '@ember-data/adapter/rest';
 
export default class TasksAdapter extends RESTAdapter {
host = 'http://localhost:3000';
 }

Serializer file-

import RESTSerializer from '@ember-data/serializer/rest';

export default class TasksSerializer extends RESTSerializer {
  normalizeResponse(store, primaryModelClass, payload, id, requestType) {
    return this.normalize(primaryModelClass, payload);
  }
}

Hi @saimun, that’s definitely a cryptic error. Could you provide some more details about your route model hook (assuming that’s what is triggering this) and also the request payload that is coming from your server? The normalizeResponse method seems like it could be problematic but I’d have to see your payload to make any sort of better recommendation.

1 Like

Response from Server -

// http://localhost:3000/tasks

[
  {
    "id": 1,
    "title": "My First Task",
    "priyority": "NORMAL",
    "createdAt": "2022-09-22T18:55:05.000Z"
  }
]

Ember Router Model -

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class IndexRoute extends Route {
  @service store;
  async model() {
    return this.store.findAll('tasks');
  }
}

Ok I’d guess that the problem is your serializer, calling normalize from normalizeResponse is kinda short-circuiting some of the serializer logic. Since your payload doesn’t include type information you might be better off just extending the JSONSerializer like this:

import JSONSerializer from '@ember-data/serializer/json';

export default class TasksSerializer extends JSONSerializer { }

The RESTSerializer doesn’t do all that much on top of the JSONSerializer (which is confusingly named because there’s also the JSONAPISerializer) but it does expect your payload to have type wrapper(s):

{
  "post": {
    "id": 1,
    "title": "Rails is omakase",
    "comments": [ 1, 2 ]
  },
  "comments": [{
    "id": 1,
    "body": "FIRST"
  }, {
    "id": 2,
    "body": "Rails is unagi"
  }]
}
1 Like

that solution solved my problem … THANKS @dknutsen . now i understand my serializer was the main problem.

1 Like