2

尝试通过 restful api 加载一组记录时出现此错误。它适用于 ember-model 但是当我切换到 ember-data (beta 2) 和 ember.js (1.0) 时,我收到了这个错误。

断言失败:找不到“0”的模型

App.ApplicationAdapter = DS.RESTAdapter.extend({
  host: 'http://localhost:8080',
  namespace: 'api'
});


App.Router.map(function() {
  this.resource('about');
  this.resource('pages', function() {
      this.resource('page', { path: ':page_id' });
  });
this.resource('login');
});

App.AuthenticatedRoute = Ember.Route.extend({
  actions: {
     error: function(reason, transition) {
      if (reason.status == 403) {
          alert('You must login');
          this.transitionTo('login');
      }
      else {
          //alert('non 403 error:'+reason.status);
          this.transitionTo('login');
      }
     }
  }
});


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

App.PageRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('page', params.page_id);
  }
});

App.Page = DS.Model.extend({
  user: DS.attr('string'),
  slug: DS.attr('string'),
  zcontent: DS.attr(),
  title: DS.attr('string'),
  create_time: DS.attr('date'),
  update_time: DS.attr('date')
});

这是后端返回的内容:

{
    "pages": [
        {
            "update_time": "2013-01-14 19:59:35.744855",
            "title": "",
            "id": 38,
            "create_time": "2012-08-04 15:08:16",
            "user": "robert-jones",
            "slug": "orange"
        },
        {
            "update_time": "2013-01-14 19:59:35.899830",
            "title": "",
            "id": 47,
            "create_time": "2012-08-03 10:04:07",
            "user": "robert-jones",
            "slug": "REVIEWStheraputics"
        },
        {
            "update_time": "2012-12-18 07:16:13.656509",
            "title": "'Notes on Temperature Heat Map'",
            "id": 17,
            "create_time": "2012-12-18 07:14:48.118119",
            "user": "robert-jones",
            "slug": "50612c27b94ca650"
        },
        {
            "update_time": "2012-12-12 19:48:08.079021",
            "title": "'Notes on Dyanmic Heat Map'",
            "id": 8,
            "create_time": "2012-12-12 19:48:08.013336",
            "user": "robert-jones",
            "slug": "c5642b4c14d901cf"
        }
    ]
}
4

1 回答 1

2

我之前遇到过这种情况,当时我的服务器只是返回一个原始数组而不是一个带有根元素的对象。也就是说,而不是这样:

{pages : [...]}

我不小心退回了这个:

[...]

没有 JSBin 就很难说。

于 2013-09-18T03:34:21.807 回答