3
votes

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.

3
Did you ever figure this out?Matt Jensen

3 Answers

4
votes

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', {...} )

0
votes

I think that your require is returning undefined.

Cat = require '../../models/cat'
Cat // the variable Cat is undefined

So, this code will fail

@store.createRecord(Cat /* Cat is undefined*/, {
  created: new Date()
})

Are you sure that the cat model is in the path ../../models/cat ?

Some build tools like ember-tools, need that you expose the class using module.exports, if you are using it, you need to use:

module.exports = Cat; 

in your cat.js file.

I hope it helps

0
votes

I updated your JSBin to work. You have to pass the string version of the model, so for App.Cat you would pass "cat" for something like App.BigPost you would pass "bigPost" the string will be resolved to a model and then the ember-data adapter will know what to do.

Working version - http://jsbin.com/ixIqIjI/10/edit