6

I'm trying to create an app using ember.js and ember-data, using the following versions:

DEBUG: Ember      : 1.7.0
DEBUG: Ember Data : 1.0.0-beta.9
DEBUG: Handlebars : 1.2.1
DEBUG: jQuery     : 2.1.0 

I'm using the RESTAdapter to connect to an api I wrote using node.js.

As soon as I load the app I keep getting the following error:

Error while processing route: students undefined is not a function TypeError: undefined is not a function
    at http://localhost:9000/scripts/vendor/ember-data.js:12006:34
    at tryCatch (http://localhost:9000/scripts/vendor/ember.js:45818:16)
    at invokeCallback (http://localhost:9000/scripts/vendor/ember.js:45830:17)
    at publish (http://localhost:9000/scripts/vendor/ember.js:45801:11)
    at http://localhost:9000/scripts/vendor/ember.js:29069:9
    at DeferredActionQueues.invoke (http://localhost:9000/scripts/vendor/ember.js:634:18)
    at Object.DeferredActionQueues.flush (http://localhost:9000/scripts/vendor/ember.js:684:15)
    at Object.Backburner.end (http://localhost:9000/scripts/vendor/ember.js:147:27)
    at Object.Backburner.run (http://localhost:9000/scripts/vendor/ember.js:202:20)
at apply (http://localhost:9000/scripts/vendor/ember.js:18382:27)

Here's the code I'm using (loaded in the same order I pasted it):

app.js

var App = window.App = Ember.Application.create({
    LOG_ACTIVE_GENERATION: true,
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: false,
    LOG_VIEW_LOOKUPS: true
});

store.js

App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000',

    serializer: DS.RESTSerializer.extend({
        primaryKey: function(type) {
            return '_id';
        },

        serializeId: function(id) {
            return id.toString();
        }
    })
});

models/student.js

App.Student  = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    nationality: DS.attr('string'),
    createdAt: DS.attr('date')
});

routes/app_route.js

App.StudentsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('student');
    }
});

router.js

App.Router.map(function () {
    this.resource('students', {path: '/'});
});

And the following is the response of the API:

{
    students: [
        {
            nationality: "Lorem",
            lastName: "Doe",
            firstName: "John",
            _id: "53f87200f3750319b4791235",
            createdAt: "2014-08-23T10:50:40.661Z"
        },
        {
            nationality: "Lorem",
            lastName: "Doe",
            firstName: "John",
            _id: "53f87299f3750319b4791234",
            createdAt: "2014-08-23T10:50:40.661Z"
        }
    ]
}

It looks like the store is not loading the data from the API, but the JSON data format looks fine. Any idea of what could be wrong?

Thanks!

2
  • I think I had the exact same error, which started when I wasn't using 1.7.0 but 1.8.0-beta.1 ... switching back to 1.7.0 fixed it for me. Not sure why you are having the problem though :( Commented Aug 27, 2014 at 15:05
  • I believe you need to use a string in primaryKey instead of a function based on the docs. Something like primaryKey: '. Commented Aug 27, 2014 at 21:02

3 Answers 3

4

So after searching more on Stack Overflow, I've figured out that the serializer has now to be in a separate class than the RESTAdapter, so the working code is the following:

store.js

App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000'
});

App.ApplicationSerializer = DS.RESTSerializer.extend({
    primaryKey: '_id',
    serializeId: function(id) {
        return id.toString();
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

For those using ember-cli, check out my answer.
3

Here's an updated answer for people using ember-cli.

ember g adapter application #=> creates app/adapters/application.js
ember g serializer application #=> creates app/serializers/application.js

In app/adapters/application.js:

import DS from 'ember-data';

export default DS.RestAdapter.extend({
  host: 'http://localhost:3000'
});

In app/serializers/application.js:

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
  primaryKey: '_id',
  serializeId: function(id) {
    return id.toString();
  }
});

Comments

0

I was getting this error, and it had nothing to do with any of the usual suspects.

In coffeescript, I had started defining a model.

 App.Cost = DS.Model.extend
   amount:         DS.attr 'number'
   active:         DS.attr 'boolean'

To create a second model, I c/p my first model in and deleted the attributes:

  App.Cost = DS.Model.extend

The went back and tried to run a seemingly unrelated model

 localhost:3000/products

Which resulted in the error

 Error while processing route: products.index

Simply making sure my model was named correctly solved the error:

 App.Cost = DS.Model.extend(...)
 App.Price = DS.Model.extend(...)   <- instead of repeating the Cost model

This was re-produceable, so I thought it might be helpful to others.

Comments

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.