【问题标题】:Error while processing route error in ember.js with ember-data使用 ember-data 在 ember.js 中处理路由错误时出错
【发布时间】:2014-08-27 14:42:08
【问题描述】:

我正在尝试使用 ember.js 和 ember-data 创建一个应用,使用以下版本:

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
});

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: '/'});
});

以下是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 数据格式看起来不错。知道可能出了什么问题吗?

谢谢!

【问题讨论】:

  • 我想我有完全相同的错误,当我没有使用 1.7.0 而是 1.8.0-beta.1 时开始......切换回 1.7.0 为我修复了它。不知道为什么你会遇到这个问题:(
  • 我相信您需要在primaryKey 中使用字符串而不是函数based on the docs。类似primaryKey: '.

标签: ember.js ember-data


【解决方案1】:

所以在 Stack Overflow 上进行了更多搜索后,我发现序列化程序现在必须位于与 RESTAdapter 不同的类中,因此工作代码如下:

store.js

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

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

【讨论】:

  • 对于那些使用ember-cli的人,请查看我的答案。
【解决方案2】:

这是使用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'
});

app/serializers/application.js:

import DS from 'ember-data';

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

【讨论】:

    【解决方案3】:

    我遇到了这个错误,它与任何常见的嫌疑人无关。

    在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
    

    这是可重现的,所以我认为它可能对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多