EmberJS:加载路由时出错:TypeError:无法读取属性'地图'为null

EmberJS: Error while loading route: TypeError: Cannot read property 'map' of null

本文关键字:属性 地图 null 读取 路由 加载 出错 TypeError EmberJS      更新时间:2023-09-26

我刚开始学习Ember,不知怎么的,我连最简单的事情都做不到。我现在使用EmberData的Fixtures来处理MVC部分,而不太关注数据检索。

到目前为止,我拥有的是:

// Engine
window.Monitor = Ember.Application.create();
Monitor.ApplicationAdapter = DS.FixtureAdapter.extend();
// Routes
Monitor.Router.map(function() {
  this.resource('processes', { path: '/' });
});
Monitor.ProcessesRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('process');
  }
});
// Data model
Monitor.Process = DS.Model.extend({
  name: DS.attr('string'),
  alt: DS.attr('string'),
  icon: DS.attr('string'),
  link: DS.attr('string')
});
// Controllers
Monitor.ProcessesController = Ember.ArrayController.extend({
  itemController: 'process',
  sortProperties: ['name'],
  sortAscending: true
});
Monitor.ProcessController = Ember.ObjectController.extend();
// Test data
Monitor.Process.FIXTURES = [
 {
  name: 'MyTestProcess',
  icon: 'link to icon'
 }
];

这是Ember 101,但我无法理解为什么简单地键入以下内容:

<div class="navtitle">
    Title
</div>
{{#each}}
<li>
    <label>{{name}}</label>
</li>
{{/each}}

失败:

Error while loading route: TypeError: Cannot read property 'map' of null
    at Ember.EnumerableUtils.map (http://www.somedomain.com/js/ember-1.5.1.js:1918:15)
    at Ember.Object.extend.pushMany (http://www.somedomain.com/js/ember-data.js:10:6083)
    at http://www.somedomain.com/js/ember-data.js:9:31369
    at invokeCallback (http://www.somedomain.com/js/ember-1.5.1.js:10013:19)
    at publish (http://www.somedomain.com/js/ember-1.5.1.js:9683:9)
    at publishFulfillment (http://www.somedomain.com/js/ember-1.5.1.js:10103:7)
    at http://www.somedomain.com/js/ember-1.5.1.js:18380:7
    at Object.DeferredActionQueues.flush (http://www.somedomain.com/js/ember-1.5.1.js:6127:24)
    at Object.Backburner.end (http://www.somedomain.com/js/ember-1.5.1.js:6215:27)
    at Object.Backburner.run (http://www.somedomain.com/js/ember-1.5.1.js:6254:18)

我做错了什么?

在您的模板中,您有{{#each}}...{{/each}},当您有一个数组作为路由模型时使用它。但您的控制器是ObjectController,因此它将数组视为单个对象,而不是原来的数组。请改为从ArrayController扩展。

因此,最终,我通过从头开始找到了解决方案。您的应用程序似乎需要有一个ApplicationRouteApplicationController

如果你没有指定车把的范围,这些将是默认使用的,如果没有定义,它们将自动生成。

在我的案例中,将所有"进程"重命名为"应用程序"解决了我的问题。