0

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'.

1
  • and what is 'type' in the extractArray function when the code is being executed ?, put a debugger or a log, and see what value it has. Commented Dec 16, 2013 at 16:27

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Sure it is, I told him where his error was. I asked him an additional question to hit 30 chars.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.