当getJSON出现时,Embjerjs store.find无法正常工作

时间:2015-03-16 16:54:48

标签: json ember.js ember-data

我开始构建一个ember应用程序。我几乎已经完成了API,我知道它可以工作。我能够使用.getJSON正确显示,但不能使用store.find方法正确显示,而是我得到一个未知类型错误,我猜测这个错误等于未正确检索的数据。我在Chrome中使用ember检查器,但我不知道它在哪里尝试获取JSON,我假设它应该是" api / projects"。

谢谢!

window.App = Ember.Application.create();

App.Router.map(function() {
  this.route('projects')
});

App.ApplicationAdapter = DS.RESTAdapter.extend({
  namespace: 'api'
});

App.Projects = DS.Model.extend({
  title: DS.attr('string'),
  description: DS.attr('string')
});

这有效:

App.ProjectsRoute = Ember.Route.extend({
   model: function(){
     return $.getJSON("api/projects").then(function(data){
       return data.projects;
     });
    }
 });

这不是:

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

ERROR:

Error while processing route: projects Cannot read property 'typeKey' of undefined TypeError: Cannot read property 'typeKey' of undefined

at DS.RESTAdapter.DS.Adapter.extend.findAll (http://trgmanage.app/js/libs/ember-data.min.js:9:28455)
at Object.Ember.assert (http://trgmanage.app/js/libs/ember-1.10.0.debug.js:3934:27)
at DS.Store.Ember.Object.extend.fetchAll (http://trgmanage.app/js/libs/ember-data.min.js:8:20000)
at DS.Store.Ember.Object.extend.findAll (http://trgmanage.app/js/libs/ember-data.min.js:8:19773)
at DS.Store.Ember.Object.extend.find (http://trgmanage.app/js/libs/ember-data.min.js:8:16174)
at App.ProjectsRoute.Ember.Route.extend.model (http://trgmanage.app/js/app.js:13:23)
at EmberObject.extend.deserialize (http://trgmanage.app/js/libs/ember-1.10.0.debug.js:23915:21)
at applyHook (http://trgmanage.app/js/libs/ember-1.10.0.debug.js:50118:30)
at Object.HandlerInfo.runSharedModelHook (http://trgmanage.app/js/libs/ember-1.10.0.debug.js:48139:22)
at Object.subclass.getModel (http://trgmanage.app/js/libs/ember-1.10.0.debug.js:48365:21)

JSON响应:(由Chrome中的JSONview格式化)

{
projects: [
{
id: 1,
title: "this is going",
description: "Aspernatur eum placeat consequuntur. Hic totam molestias nemo. Aut et aut aut earum deserunt impedit. Voluptatum omnis quae est corporis dicta voluptas ut quod. Laboriosam impedit sed eos ut rerum aliquid."
},
{
id: 2,
title: "twinkle1",
description: "Atque nihil ratione dolorem neque odio ex. Facere eaque veritatis incidunt veniam vitae. Culpa voluptatum accusamus voluptate voluptate aut nihil. Sunt adipisci deleniti nihil quia dolorem. Dolor nemo et explicabo minima eos iusto autem. Non qui eveniet rem aliquid et."
},
{
id: 3,
title: "Culpa quibusdam perspiciatis id dolorem consequuntur.",
description: "Ut veritatis nihil ducimus consequuntur nulla. Facilis qui error enim iste soluta dolore. Optio accusamus aliquam dicta. Voluptatem sint dolores quaerat quam ut. Deleniti iste quae molestias amet velit."
}
   ]
}

JSON响应未格式化:

{"projects":[{"id":1,"title":"this is going","description":"Aspernatur eum placeat consequuntur. Hic totam molestias nemo. Aut et aut aut earum deserunt impedit. Voluptatum omnis quae est corporis dicta voluptas ut quod. Laboriosam impedit sed eos ut rerum aliquid."},{"id":2,"title":"twinkle1","description":"Atque nihil ratione dolorem neque odio ex. Facere eaque veritatis incidunt veniam vitae. Culpa voluptatum accusamus voluptate voluptate aut nihil. Sunt adipisci deleniti nihil quia dolorem. Dolor nemo et explicabo minima eos iusto autem. Non qui eveniet rem aliquid et."},{"id":3,"title":"Culpa quibusdam perspiciatis id dolorem consequuntur.","description":"Ut veritatis nihil ducimus consequuntur nulla. Facilis qui error enim iste soluta dolore. Optio accusamus aliquam dicta. Voluptatem sint dolores quaerat quam ut. Deleniti iste quae molestias amet velit."}]}

1 个答案:

答案 0 :(得分:0)

糟糕。使用旧版本的ember-data beta 6. yikes。更新到最新的测试版15后,一切正常。