'todo' was saved to the server, but the response does not have an id and your record does not either

Hi,

I’m facing this error since yesterday, Error: Assertion Failed: 'todo' was saved to the server, but the response does not have an id and your record does not either.

I know it should come from app/serializers/todo.js or my app/routes/application.jsbut after looking into severals forum, I have to ask the question to expert emberJs dev, as i’m a newbie :smiley:

Here’s my app/serializers/todo.js:

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  serialize: function(record, options) {
    var json = this._super.apply(this, arguments); // Get default serialization

    json.id = record.id;  // tack on the id

    return json;
  }
});

And my app/routes/application.js

import Route from '@ember/routing/route';

export default Route.extend({
  model(){
    return this.store.findAll('todo');
  },
    actions: {
      createtodo: function() {

        var titleInput = this.get("newTitle")
        var todo = this.store.createRecord('todo', {
          title: titleInput,
          isCompleted: false
        }); 

        this.set('newTitle', '');
        todo.save();
      }
    }
});

The way the createtodo action is triggered app/templates/application.hbs:

{{input type="text" id="new-todo" value=newTitle}}
<button {{action "createtodo"}}>Save</button>

and my app/adapters/application.js :

import ActiveModelAdapter from 'active-model-adapter';

 export default ActiveModelAdapter.extend({

});

So my objec is created but not save. When i’m looking into my ember Inspector, I see that each object I create have an ID but the title field is null or "".

This is a todoApp with an Rails-API as back and Ember as front.

Anyone see what’s wrong here ?

Hi @Raphael_FOSSE, I think the typical way to use a Rails backend is to let the server (Rails) pick the id. So you would not need to try to set it in the serializer.

It sounds likely that the response you’re getting from the server is not formatted exactly the way your app is expecting. What do you have in app/adapters/todo.js and/or app/adapters/application.js?

If you have choices over how the Rails server works, you might want to use http://jsonapi-resources.com/, which would make your server json:api spec compliant, which would make it work with ember-data with nearly no configuration.

Hi @ef4, many thanks for you answer.

I’ll update my question to show you what’s in my app/adapters/application.js but I dont have any app/adapters/todo.js. Should I add one ?

I’m trying to use jsonapi-rails for now.

Ah, so I see you’re using active-model-adapter. It’s README says that if you want to make your own serializer (like you’re doing in app/serializers/todo.js), you need to extend the serializer provided by active-model-adapter:

import { ActiveModelSerializer } from 'active-model-adapter';
export default ActiveModelSerializer.extend({
  ...
});

Most likely your problem is mixing a JSONSerializer with the ActiveModelAdapter.