Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember errors "Uncaught TypeError: Cannot read property 'typeKey' of undefined " on model save

Tags:

ember.js

My RESTAdapter config:

App.ApplicationAdapter = DS.RESTAdapter.extend
  namespace: '/api'

My model:

module.exports = App.Cat = DS.Model.extend
  name: DS.attr 'string'
  description: DS.attr 'string'
  picture: DS.attr 'string'
  location: DS.attr 'string'
  profileStyle: DS.attr 'string'
  created: DS.attr 'date'

My route:

Cat = require '../../models/cat'

App.CatNewRoute = Em.Route.extend
  model: ->
    @store.createRecord(Cat, {
      created: new Date()
    })

My controller:

App.CatNewController = Em.ObjectController.extend
  actions:
    addCat: ->
      console.log 'saving...'
      @get('model').save().then ->
        console.log 'all done'

I've verified with the Ember inspector that my model has all the attributes that get assigned through a form on the template at the time of save (except for an id which is null - I think this is normal and Ember by default uses the ID assigned to a model by the server). Whenever I save the model I get the error:

Uncaught TypeError: Cannot read property 'typeKey' of undefined 

This error occurs here in the ember data source code:

if (container) {
  adapter = container.lookup('adapter:' + type.typeKey) || container.lookup('adapter:application');
} 

I'm using Ember-data 1.0.0 beta 3-2 and Ember 1.0.0-4. I've tried a few different versions of Ember data to the same effect.

Close approximation in jsBin: http://jsbin.com/ixIqIjI/6/edit?html,js,console,output

This jsBin also errors with the typeKey error.

like image 848
polyrhythm Avatar asked Dec 29 '25 01:12

polyrhythm


1 Answers

I found the solution. createRecord takes type and record arguments, but the type can not be the explicit Model name like App.Cat, it has to be a string which Ember then attaches to the correct model based on the name. In this case, the correct way to create the record is this.store.createRecord('cat', {...} )

like image 159
polyrhythm Avatar answered Dec 30 '25 22:12

polyrhythm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!