在ember.js中使用ember数据处理路由错误时出错

在ember.js中使用ember数据处理路由错误时出错,ember.js,ember-data,Ember.js,Ember Data,我正在尝试使用以下版本创建一个使用ember.js和ember数据的应用程序: DEBUG: Ember : 1.7.0 DEBUG: Ember Data : 1.0.0-beta.9 DEBUG: Handlebars : 1.2.1 DEBUG: jQuery : 2.1.0 我正在使用RESTAdapter连接到我使用node.js编写的api 当我加载应用程序时,我不断收到以下错误: Error while processing route: students u

我正在尝试使用以下版本创建一个使用ember.js和ember数据的应用程序:

DEBUG: Ember      : 1.7.0
DEBUG: Ember Data : 1.0.0-beta.9
DEBUG: Handlebars : 1.2.1
DEBUG: jQuery     : 2.1.0 
我正在使用RESTAdapter连接到我使用node.js编写的api

当我加载应用程序时,我不断收到以下错误:

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)
下面是我正在使用的代码(按粘贴顺序加载):

app.js

var App = window.App = Ember.Application.create({
    LOG_ACTIVE_GENERATION: true,
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: false,
    LOG_VIEW_LOOKUPS: true
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000',

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

        serializeId: function(id) {
            return id.toString();
        }
    })
});
App.Student  = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    nationality: DS.attr('string'),
    createdAt: DS.attr('date')
});
App.StudentsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('student');
    }
});
App.Router.map(function () {
    this.resource('students', {path: '/'});
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000'
});

App.ApplicationSerializer = DS.RESTSerializer.extend({
    primaryKey: '_id',
    serializeId: function(id) {
        return id.toString();
    }
});
store.js

var App = window.App = Ember.Application.create({
    LOG_ACTIVE_GENERATION: true,
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: false,
    LOG_VIEW_LOOKUPS: true
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000',

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

        serializeId: function(id) {
            return id.toString();
        }
    })
});
App.Student  = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    nationality: DS.attr('string'),
    createdAt: DS.attr('date')
});
App.StudentsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('student');
    }
});
App.Router.map(function () {
    this.resource('students', {path: '/'});
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000'
});

App.ApplicationSerializer = DS.RESTSerializer.extend({
    primaryKey: '_id',
    serializeId: function(id) {
        return id.toString();
    }
});
模型/student.js

var App = window.App = Ember.Application.create({
    LOG_ACTIVE_GENERATION: true,
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: false,
    LOG_VIEW_LOOKUPS: true
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000',

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

        serializeId: function(id) {
            return id.toString();
        }
    })
});
App.Student  = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    nationality: DS.attr('string'),
    createdAt: DS.attr('date')
});
App.StudentsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('student');
    }
});
App.Router.map(function () {
    this.resource('students', {path: '/'});
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000'
});

App.ApplicationSerializer = DS.RESTSerializer.extend({
    primaryKey: '_id',
    serializeId: function(id) {
        return id.toString();
    }
});
路由/app_route.js

var App = window.App = Ember.Application.create({
    LOG_ACTIVE_GENERATION: true,
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: false,
    LOG_VIEW_LOOKUPS: true
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000',

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

        serializeId: function(id) {
            return id.toString();
        }
    })
});
App.Student  = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    nationality: DS.attr('string'),
    createdAt: DS.attr('date')
});
App.StudentsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('student');
    }
});
App.Router.map(function () {
    this.resource('students', {path: '/'});
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000'
});

App.ApplicationSerializer = DS.RESTSerializer.extend({
    primaryKey: '_id',
    serializeId: function(id) {
        return id.toString();
    }
});
router.js

var App = window.App = Ember.Application.create({
    LOG_ACTIVE_GENERATION: true,
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: false,
    LOG_VIEW_LOOKUPS: true
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000',

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

        serializeId: function(id) {
            return id.toString();
        }
    })
});
App.Student  = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    nationality: DS.attr('string'),
    createdAt: DS.attr('date')
});
App.StudentsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('student');
    }
});
App.Router.map(function () {
    this.resource('students', {path: '/'});
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000'
});

App.ApplicationSerializer = DS.RESTSerializer.extend({
    primaryKey: '_id',
    serializeId: function(id) {
        return id.toString();
    }
});
以下是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"
        }
    ]
}
看起来存储没有从API加载数据,但是JSON数据格式看起来不错。你知道会出什么问题吗


谢谢

因此,在搜索了更多关于堆栈溢出的内容后,我发现序列化程序现在必须位于与RESTAdapter不同的类中,因此工作代码如下所示:

store.js

var App = window.App = Ember.Application.create({
    LOG_ACTIVE_GENERATION: true,
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: false,
    LOG_VIEW_LOOKUPS: true
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000',

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

        serializeId: function(id) {
            return id.toString();
        }
    })
});
App.Student  = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    nationality: DS.attr('string'),
    createdAt: DS.attr('date')
});
App.StudentsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('student');
    }
});
App.Router.map(function () {
    this.resource('students', {path: '/'});
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000'
});

App.ApplicationSerializer = DS.RESTSerializer.extend({
    primaryKey: '_id',
    serializeId: function(id) {
        return id.toString();
    }
});

我犯了这个错误,而这与通常的嫌疑犯无关

在coffeescript中,我开始定义一个模型

 App.Cost = DS.Model.extend
   amount:         DS.attr 'number'
   active:         DS.attr 'boolean'
要创建第二个模型,请在中对第一个模型进行c/p,并删除以下属性:

  App.Cost = DS.Model.extend
他们回去尝试运行一个看似无关的模型

 localhost:3000/products
这导致了错误

 Error while processing route: products.index
只需确保我的模型名称正确,即可解决此错误:

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

App.Price=DS.Model.extend(…)这是一个更新的答案,适用于使用
ember cli的用户

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

import DS from 'ember-data';

export default DS.RestAdapter.extend({
  host: 'http://localhost:3000'
});
import DS from 'ember-data';

export default DS.RESTSerializer.extend({
  primaryKey: '_id',
  serializeId: function(id) {
    return id.toString();
  }
});
在app/serializers/application.js中

import DS from 'ember-data';

export default DS.RestAdapter.extend({
  host: 'http://localhost:3000'
});
import DS from 'ember-data';

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

我想我也犯了同样的错误,那是在我不使用1.7.0而是使用1.8.0-beta.1时开始的。。。切换回1.7.0为我修复了它。但不确定您为什么会遇到问题:(我认为您需要在
primaryKey
中使用字符串,而不是函数。类似于
primaryKey:'
。对于使用
ember cli
的用户,请查看我的答案。