简体   繁体   中英

Emberjs: cannot read property 'type' of undefined

I have created a model in my ember app called ticket-stats:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export default Model.extend({

    get_tickets_more: attr(),
    get_tickets: attr(),
    get_avg_tickets: attr()

});

The data is pulled from JSON api: http://domain.com/data/ticketStats?blah=blah ... So i have added a special adapter for this model called ticket-stats:

import JSONAPIAdapter from 'ember-data/adapters/json-api';

export default JSONAPIAdapter.extend({

    host: 'http://domain.com',
    namespace: 'data',

    pathForType: function(type) {
        return Ember.String.camelize(type);
    }
});

I get the data for this model in route:

import Ember from 'ember';

export default Ember.Route.extend({

    model () {
        var ticketData;
        this.store.query('ticket-stats', { teamID: 218, attUID: 'oc7569', useProd: 1})
                .then(function(stats) { ticketData = stats; });

        return Ember.RSVP.hash({
            currentUser: this.currentUser,
            ticketStats: ticketData
        });
    }
});

And, i get a TypeError:

ember.debug.js:32096 TypeError: Cannot read property 'type' of undefined
    at _pushInternalModel (store.js:1524)
    at push (store.js:1501)
    at finders.js:171
    at Object.Backburner.run (ember.debug.js:678)
    at _adapterRun (store.js:1733)
    at finders.js:168
    at tryCatch (ember.debug.js:53806)
    at invokeCallback (ember.debug.js:53821)
    at publish (ember.debug.js:53789)
    at ember.debug.js:32054onerrorDefault @ ember.debug.js:32096exports.default.trigger @ ember.debug.js:54476(anonymous function) @ ember.debug.js:55727Queue.invoke @ ember.debug.js:333Queue.flush @ ember.debug.js:397DeferredActionQueues.flush @ ember.debug.js:205Backburner.end @ ember.debug.js:560(anonymous function) @ ember.debug.js:1126

Any ideas as to why this is happening? This error goes away when i remove the pathForType function in the adapter, but then i get another error about getting the data from http://domain.com/data/ticket-stats ?... which is not the correct URL. I have to convert to camelCase, ticket-stats => ticketStats.

This is what my json looks like:

{
   "get_avg_tickets": { ... },
   "get_tickets_more": { ... },
   "get_tickets": { ... }
}

I also modified the application serializer by simply replacing JSONAPISerializer with JSONSerializer: app/serializers/application.js

import JSONSerializer from 'ember-data/serializers/json';

export default JSONSerializer.extend({
});

Any help would be appreciated! I'm very new to Ember.

you need to use this serializer here is the reference link http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html

import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  normalizeQueryResponse(store, primaryModelClass, payload, id, requestType) {
    payload = { ticketStats: payload };
    return this.normalizeArrayResponse(store, primaryModelClass, payload, id, requestType);
  },
});

your json should be in this format:

{
   "get_avg_tickets": 45,
   "get_tickets_more": propertyvalue,
   "get_tickets": propertyvalue
}

otherwise you need to normalize your response in normalizeQueryreponse of serilializer

Also ready following doc for your help http://thejsguy.com/2015/12/05/which-ember-data-serializer-should-i-use.html i hope it will help you. dont forget to accept my answer.

you need to use serializers which are best suited for your json data.

http://emberjs.com/api/data/classes/DS.JSONAPISerializer.html

This error is raising because your json data received from api is not fully fit into you application requirements for json formate.

Please Share json data here. because domain.com json url is not working. there are so many issues in your code. let me to guide you one by one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM