Response does not have an id and your record does not either


#1

I’m working on a project with Ember and a Python back-end. At the back-end I’m working with the marshmallow-jsonapi to provide a JSON API-compliant response. At the ‘motoristas/new’ route I’m sending a POST request that gets as response an object with the response code and the following body:

{  
   'data':[  
      {  
         'id':'145',
         'attributes':{  
            'titulo-eleitor':'2132.1321.3213',
            'nome-mae':'asdasda',
            'identidade':'131231',
            'naturalidade':'Amapá',
            'cpf':'123.213.213-12',
            'enderecos':{  
               'data':[  
                  {  
                     'id':'112',
                     'attributes':{  
                        'estado':'AP',
                        'motorista-id':'145',
                        'cidade':'asdsadsa',
                        'logradouro':'asdsadsa',
                        'cep':'12.321-321',
                        'bairro':'asdasdsa'
                     },
                     'type':'enderecos_motorista'
                  }
               ]
            },
            'data-admissao':'2017-05-08T03:00:00+00:00',
            'nome':'asdsada',
            'nome-pai':'sdasdsa',
            'email':'holanda.kamilla@gmail.com',
            'pis-pasep':'212.32131.23-2',
            'cnh':{  
               'data':[  
                  {  
                     'id':'119',
                     'attributes':{  
                        'motorista-id':'145',
                        'categoria':'D',
                        'numero':'12321321321',
                        'data-vencimento':'2017-05-08 03:00:00'
                     },
                     'type':'cnh_motorista'
                  }
               ]
            },
            'matricula':'12312',
            'carteira-trabalho':'2312321321',
            'telefones':{  
               'data':[  
                  {  
                     'id':'111',
                     'attributes':{  
                        'motorista-id':'145',
                        'numero':'21313-1213',
                        'ddd':'111',
                        'tipo-telefone':'Residencial'
                     },
                     'type':'telefones_motorista'
                  }
               ]
            },
            'cargo-id':'131',
            'orgao-emissor-id':'SSPDF',
            'escolaridade':'Ensino Fundamental',
            'nome-conjuge':None,
            'estado-id':'130',
            'data-emissao-ct':'2017-05-08 03:00:00',
            'estado-civil':'Solteiro(a)',
            'conta-bancaria':{  
               'data':[  
                  {  
                     'id':'114',
                     'attributes':{  
                        'tipo-conta':'Conta Corrente',
                        'banco':'001',
                        'motorista-id':'145',
                        'agencia':'12321321',
                        'numero-conta':'21321321'
                     },
                     'type':'conta_bancaria_motorista'
                  }
               ]
            }
         },
         'type':'motoristas'
      }
   ]
}

But I’m getting this error message: “Assertion Failed: ‘motorista’ was saved to the server, but the response does not have an id and your record does not either.”

I’m using the JSONAPIAdapter since I’m supposed to get a JSON API format compliant response:

import DS from 'ember-data';
import Ember from 'ember';
import ENV from 'sistran-frontend/config/environment';

export default DS.JSONAPIAdapter.extend({
  namespace: ENV.APP.namespace,

  pathForType(modelName) {
    if (modelName === 'ordem-de-servico') {
      return 'ordens-de-servico';
    }

    if (modelName === 'veiculo') {
      return 'veiculos';
    }

    if (modelName === 'motorista') {
      return 'motoristas';
    }
  },
});

I’m not sure why I’m getting this message because I have the id field on the response. What is wrong with the response that I’m sending to the Ember app? I have the ‘motoristas/index’ route that gets the same response object with the same body format and It’s working.


#2

The only hunch that I have is the response is returning a data: [] when it should be returning a data: {}. Other than that everything looks about right… :confused:


#3

Thank you for your answer! Turns out it was an api problem. I was really returning an array. I just changed it to return only one registry and everything works fine now!


#4

I have the similar problem when using Rails 5 API on the backend. I also have the same data structure in post response:

{
	"data": [{
		"id": "419",
		"type": "languages",
		"attributes": {
			"tag": "ru-RU"
		}
	}, {
		"id": "374",
		"type": "languages",
		"attributes": {
			"tag": "nl-BE"
		}
	}, {
		"id": "245",
		"type": "languages",
		"attributes": {
			"tag": "fr-FR"
		}
	}]
}

And JSONLint validates it with no problem. So, I don’t think it to be an API problem. Any other tips or solutions to fix it ? Thank you.


#5

@belgoros What are you using for that model’s serializer? Either that specific model or your applications’s serializer looks like it needs to be JSONAPI. Here is more information in the docs: https://guides.emberjs.com/v3.0.0/models/customizing-serializers/


#6

I use AMS gem. I returned an array from the Rails controller:

def create
    @shop.languages << @language
    json_response @shop.languages, :created
end

After changing to return a created language, it works as needed:

  def create
    @shop.languages << @language
    json_response @language, :created
  end

I have no custom serializer defined in Ember app.


#7

That makes sense. Your creates should return a single model, not an array of them.