Loopo
Loopo

Reputation: 2195

ember data debug model deserialisation

I have an ember application that is getting its data from a REST API.

I have a particular model contacts that is not being processed correctly.

There are no error messages such as you get if you spell the model name wrong. The payload for the index route contains about 1500 contacts like so:

{"contacts":
   [{"id":1,
     "type":"customer",
     "foreign_key":1242,
     "first_name":"John",
     "last_name":"Smith",
     ...
     "created_at":"-0001-11-30 00:00:00",
     "updated_at":"-0001-11-30 00:00:00",
     "customer_id":1242
    },
     ...
   ]
}

The JSON response looks OK when I look in the Network tab. The browser is able to parse and display the response nicely.

I have compared the response to similar models and the format looks the same to me.

In the ember inspector, under Data I get 1 contact with id=null and other fields undefined.

The contact model is:

import DS from 'ember-data';
export default DS.Model.extend({
  type:           DS.attr('string'),
  foreign_key:    DS.attr('number'),
  first_name:     DS.attr('string'),
  last_name:      DS.attr('string'),
  ... more strings 
  created_at:     DS.attr('string'),
  updated_at:     DS.attr('string'),
  customer:       DS.belongsTo('customer')  
});

ember 2.0.2
ember-data 2.0.0
using pod structure

How can I investigate further? It looks like the response is not being parsed and turned into contact models properly.

ANSWER

It turns out that you shouldn't use type as one of your field names in the model.
(I should have gone with the small warning bell that I heard when defining my DB table... )

Upvotes: 0

Views: 110

Answers (1)

pjcarly
pjcarly

Reputation: 426

try adding the normalizeArrayResponse in your Application Serializer, and debug like so:

normalizeArrayResponse: function(store, primaryModelClass, payload, id, requestType) {
  console.log(payload);
  return this._super(store, primaryModelClass, normalizedRecords, id, requestType);
}

Just to confirm your data is going through the serializer.

Upvotes: 1

Related Questions