aham
aham

Reputation: 137

Ember js belongs to relationship

I have two models employee and empdetails

//employee model
import DS from 'ember-data';
export default DS.Model.extend({
    empId : DS.attr(),
    password : DS.attr(),
    email : DS.attr(),
    empdetails : DS.belongsTo("empdetails") 
});

//empdetails model
import DS from 'ember-data';    
export default DS.Model.extend({
    firstName : DS.attr(),
    lastName : DS.attr(),
    dateOfJoining: DS.attr(),
    employee : DS.belongsTo("employee")
});

I use RESTAdapter to make REST calls.

//serializer
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
});

When I try to make a get request for employee I get the below error

Please check your serializer and make sure it is serializing the relationship payload into a JSON API format. Error: Assertion Failed: Encountered a relationship identifier without a type for the belongsTo relationship 'empdetails' on , expected a json-api identifier with type 'empdetails' but found '{"id":"1","firstName":"xxx"}

I get the below JSON from my backend

[
  {
    "id": 1,
    "email": "[email protected]",
    "password": "12345678",
    "empdetails": {
      "id": 1,
      "firstName": "xxx",
      "lastName": "yyy",
      "dateOfJoining": "22-10-2018"
    }
  }
]

Can someone guide me in rectifying the error

Upvotes: 2

Views: 2138

Answers (2)

user7474631
user7474631

Reputation:

If you are using Django Rest as the Backend, then please use DRF Adapter and DRF Serializer

Visit here for more Information

Upvotes: 0

Andrei Piatrou
Andrei Piatrou

Reputation: 434

Try to update your employee serializer as follows:

import DS from 'ember-data';

export default DS.JSONSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        empdetails: {
            serialize: 'records',
            deserialize: 'records'
        }
}});

See this article for more details.

Upvotes: 7

Related Questions