Search code examples
coffeescriptember-dataember-cli

ember-cli - Ember Data get request failed TypeError - undefined is not a function


I'm porting the ember portion of an ember-rails app to Ember CLI. So far so good, but I'm having trouble with a get request that's blowing up in RESTSerializer.

The Application Controller attempts to grab a list of widgets

`import Ember from 'ember'`
ApplicationController = Ember.Controller.extend
  widgets: (->
    @store.find('unfinishedWidgets', id: @get('currentId'))
  ).property()

`export default ApplicationController`

I can see the API Request fire off, the JSON comes back, everything looks just like it did in the Ember Rails stack. Except then instead of updating the property and displaying in the view, it blows up:

 TypeError: undefined is not a function
        at Object.func (http://localhost:4200/myapp/assets/vendor.js:49473:18)
        at Object.Cache.get (http://localhost:4200/myapp/assets/vendor.js:25091:38)
        at decamelize (http://localhost:4200/myapp/assets/vendor.js:49515:31)
        at RESTSerializer.extend.keyForAttribute (http://localhost:4200/myapp/assets/vendor.js:66565:16)
        at apply (http://localhost:4200/myapp/assets/vendor.js:32821:27)
        at superWrapper [as keyForAttribute] (http://localhost:4200/myapp/assets/vendor.js:32393:15)
        at null.<anonymous> (http://localhost:4200/myapp/assets/vendor.js:69024:31)
        at null.<anonymous> (http://localhost:4200/myapp/assets/vendor.js:71513:20)
        at cb (http://localhost:4200/myapp/assets/vendor.js:29067:22)
        at OrderedSet.forEach (http://localhost:4200/myapp/assets/vendor.js:28865:13) vendor.js:28532logToConsole vendor.js:28532RSVP.onerrorDefault vendor.js:42608__exports__.default.trigger vendor.js:61072Promise._onerror vendor.js:62084publishRejection vendor.js:60315(anonymous function) vendor.js:42583DeferredActionQueues.invoke vendor.js:13853DeferredActionQueues.flush vendor.js:13923Backburner.end vendor.js:13309Backburner.run vendor.js:13364run vendor.js:31375hash.success vendor.js:68006fire vendor.js:3237self.fireWith vendor.js:3349done vendor.js:9393callback

I put in a breakpoint at decamelize, which stopped so I could inspect what was going on:

function decamelize(str) {
  return DECAMELIZE_CACHE.get(str);
}

str at this point isn't a string, it's:

Object {type: undefined, isAttribute: true, options: Object, parentType: function, name: "bundleId"}
  isAttribute: true
  name: "otherId"
  options: Object
  parentType: (subclass of DS.Model)
  type: undefined__proto__: Object

So this is the first DS.attr() in my Model:

`import DS from 'ember-data'`
unfinishedWidgets = DS.Model.extend
  otherId: DS.attr()
  # other attrs
`export default UnsubmittedRequest`

I'm using a ActiveModelAdapter by default, and I've also made an empty ActiveModelSerializer.

`import DS from 'ember-data'`
ApplicationAdapter = DS.ActiveModelAdapter.extend
  namespace: 'api/myapp/v1' 
`export default ApplicationAdapter`

`import DS from 'ember-data'`
ApplicationSerializer = DS.ActiveModelSerializer.extend()
`export default ApplicationSerializer`

EDIT:

I ended up fixing it with:

ApplicationSerializer = DS.ActiveModelSerializer.extend
  keyForAttribute: (type, name) ->
    name

Though I'm still not clear on why it was necessary in Ember CLI when it was fine in Ember-rails


Solution

  • So, as it turns out, it was simply a bug in EmberData 1.0.0.beta.10. I updated to beta 12 and everything worked.