I'm working with an object called "Residence" that consists of various details. I am trying to use the serializer "extractSingle" to establish its relationships when receiving data from the server, but I keep encountering the error message "Unable to get property 'toString' of undefined or null reference". Could someone please point out where I may be going wrong in my approach?
App.Residence = DS.Model.extend({
residence_ID: attr('number'),
house_Number: attr('string'),
street: attr('string'),
city_ID: attr('number'),
state_ID: attr('number'),
zip: attr('string'),
apt: attr('string'),
client_ID: attr('number'),
client: null,
lead: null,
projectDetails: DS.hasMany('projectDetail')
});
App.ResidenceSerializer = DS.WebAPISerializer.extend({
primaryKey: 'residence_ID',
extractSingle: function (store, primaryType, payload) {
var primaryTypeName = primaryType.typeKey;
var typeName = primaryTypeName,
type = store.modelFor(typeName);
var data = {};
data[typeName] = payload;
data.projectDetails = [];
var normalizedArray = payload.projectDetails.map(function (nav) {
data.projectDetails.push(nav);
}, this);
payload.projectDetails = payload.projectDetails.mapProperty('projectDetailID');
payload = data;
return this._super.apply(this, arguments);
},
normalizeHash: {
projectDetails: function (hash) {
hash.residence_ID = hash.id;
hash.id = hash.projectDetailID;
return hash;
},
residence: function (hash) {
hash.residence_ID = hash.id;
return hash;
}
}
});
Here is a snippet of the JSON response retrieved from the server:
{
"residence_ID":3532,
"house_Number":"243",
"street":"Main St.",
"city_ID":2,
"state_ID":33,
"zip":"11111",
"apt":"35",
"client_ID":3598,
"projectDetails":[{"projectDetailID":4947,"residence_ID":3532"}, {"projectDetailID":4947,"residence_ID":3532}]
}