bookthief
bookthief

Reputation: 2461

Ember.js error while loading route

I have an ember app with two models, user and subject, both of which rely on an api for data. The JSON returned is not in the format that ember expects, so I have a custom serializer for each model-

var foo = {

    'App.Subject': {'single':'subject','array':'subjects'}

};

App.SubjectSerializer = DS.RESTSerializer.extend({
  extractSingle: function(store, type, payload, id, requestType) {
    var p = {};
    p[foo[type]['single']] = payload;
    return this._super(store, type, p, id, requestType);
  },
  extractArray: function(store, type, payload, id, requestType) {
    var p = {};
    p[foo[type]['array']] = payload;
    return this._super(store, type, p, id, requestType);
  },
  serializeIntoHash: function(hash, type, record, options) {
    Ember.merge(hash, this.serialize(record, options));
  }
});

This works perfectly. However, I have a similar serializer for my Users model-

var foo = {

    'App.User': {'single':'user','array':'users'}

};

App.UserSerializer = DS.RESTSerializer.extend({
  extractSingle: function(store, type, payload, id, requestType) {
    var p = {};
    p[foo[type]['single']] = payload;
    return this._super(store, type, p, id, requestType);
  },
  extractArray: function(store, type, payload, id, requestType) {
    var p = {};
    p[foo[type]['array']] = payload;
    return this._super(store, type, p, id, requestType);
  },
  serializeIntoHash: function(hash, type, record, options) {
    Ember.merge(hash, this.serialize(record, options));
  }
});

When I try to access my users in the browser, I get this error:

Assertion failed: Error while loading route: TypeError: Cannot read property 'array' of undefined

Can anyone help?

EDIT: 'type' in the extractArray function refers to the root property of the JSON, eg if my JSON was

{
  "user": {
    name: "xyz",
    email: "[email protected]"
  }
}

the 'type' would be 'user'.

Upvotes: 0

Views: 691

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

mappings[type] is undefined. What is mappings?

BTW, there is an easier way to do this,

single

p[type.typeKey] = payload;

array

p[Ember.String.pluralize(type.typeKey)] = payload;

Upvotes: 1

Related Questions