2014-08-27 88 views
5

我试图创建一个使用ember.js和灰烬数据的应用程序,使用以下版本:错误在处理路由错误与烬数据ember.js

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) 

下面是我使用的代码(在我粘贴它相同的顺序加载):

应用。 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(); 
     } 
    }) 
}); 

型号/ student.js

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

路线/ 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数据格式看起来不错。任何想法可能是错的?

谢谢!

+0

我觉得我有完全相同的错误,这开始时,我没有使用1.7.0但1.8.0 -beta.1 ...切换回1.7.0修复了我。不知道为什么你有这个问题,但:( – Leeft 2014-08-27 15:05:56

+0

我相信你需要在'primaryKey'中使用一个字符串,而不是一个函数[基于文档](http://emberjs.com/api/data/classes/DS .RESTSerializer.html#property_primaryKey)。例如'primaryKey:''。 – 2014-08-27 21:02:34

回答

3

所以堆栈溢出搜索更长时间后,我已经想通了,串行器现在已经是在比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(); 
    } 
}); 
+0

对于那些使用'ember-cli'的人,请查看我的答案。 – 2015-05-12 00:30:12

0

我得到这个错误,并且它与任何常见的嫌疑犯无关。

在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 

这再次produceable,所以我想这可能是帮助他人。

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