加载路由时出错:错误:断言失败:来自findAll的响应必须是数组,而不是未定义

时间:2014-02-18 14:16:53

标签: json api ember.js ember-data

我在尝试从JSON API返回结果时在控制台中收到以下错误:

加载路由时出错:错误:断言失败:来自findAll的响应必须是数组,而不是未定义

Ember Data包含来自我的JSON API的数据(见图)

View of Ember Data from the Ember Chrome inspector

除此之外,我的代码没有丢失任何错误。

有什么想法吗?

EMBER APP:

App = Ember.Application.create();

App.ApplicationAdapter = DS.RESTAdapter;

App.ApplicationAdapter = DS.RESTAdapter.extend({
    url: 'http://127.0.0.1:3000',
    namespace: 'api'    
});

App.ApplicationSerializer = DS.RESTSerializer.extend({  
    primaryKey: '_id'
});

App.Store = DS.Store.extend({
    revision: 11,
    adapter: 'App.Adapter'
});

App.Users = DS.Model.extend({
    "firstName": DS.attr('string'),
    "secondName": DS.attr('string'),
    "shortName": DS.attr('string'),
    "emailAddress": DS.attr('string'),
    "password": DS.attr('string'),
    "group": DS.attr('string'),
    "charisma": DS.attr('string'),
    "focus": DS.attr('string'),
    "strength": DS.attr('string'),
    "dexterity": DS.attr('string'),
    "userType": DS.attr('string'),
    "dressType": DS.attr('string'),
    "skinType": DS.attr('string'),
    "__v": DS.attr('string'),
    "loggedInDate": DS.attr('string'),
    "loggedIn": DS.attr('string'),
    "dateRegistered": DS.attr('string')
});

App.User = DS.Model.extend({
    "firstName": DS.attr('string'),
    "secondName": DS.attr('string'),
    "shortName": DS.attr('string'),
    "emailAddress": DS.attr('string'),
    "password": DS.attr('string'),
    "group": DS.attr('string'),
    "charisma": DS.attr('string'),
    "focus": DS.attr('string'),
    "strength": DS.attr('string'),
    "dexterity": DS.attr('string'),
    "userType": DS.attr('string'),
    "dressType": DS.attr('string'),
    "skinType": DS.attr('string'),
    "__v": DS.attr('string'),
    "loggedInDate": DS.attr('string'),
    "loggedIn": DS.attr('string'),
    "dateRegistered": DS.attr('string')
});


App.Router.map(function() {
  // put your routes here
});

App.IndexRoute = Ember.Route.extend({
    renderTemplate: function () {
        this.render();
        this.render('nav', {
            outlet: 'nav'
        });
    },
    model: function() {
        return this.store.find('users');
    }
});

JSON

{
  "Users": [
    {
      "firstName": "Berniece",
      "secondName": "Bergstrom",
      "shortName": "april",
      "emailAddress": "alverta@culpa.info",
      "password": "PRPOP",
      "group": 1,
      "charisma": 4,
      "focus": 3,
      "strength": 3,
      "dexterity": 1,
      "userType": 1,
      "dressType": 2,
      "skinType": 1,
      "_id": "5302604e1a41219a2d2072eb",
      "__v": 0,
      "loggedInDate": "2014-02-17T19:17:34.725Z",
      "loggedIn": false,
      "dateRegistered": "2014-02-17T19:17:34.724Z"
    },
    {
      "firstName": "Lamar",
      "secondName": "Schneider",
      "shortName": "august.blick",
      "emailAddress": "sandra@dolores.biz",
      "password": "PJPUK",
      "group": 0,
      "charisma": 2,
      "focus": 3,
      "strength": 1,
      "dexterity": 2,
      "userType": 1,
      "dressType": 2,
      "skinType": 2,
      "_id": "5302604e1a41219a2d2072ec",
      "__v": 0,
      "loggedInDate": "2014-02-17T19:17:34.728Z",
      "loggedIn": false,
      "dateRegistered": "2014-02-17T19:17:34.728Z"
    }
}

1 个答案:

答案 0 :(得分:2)

除非你需要用户模型用于别的东西,否则我会抛弃它而只使用用户模型。并更改此代码:

App.IndexRoute = Ember.Route.extend({
renderTemplate: function () {
    this.render();
    this.render('nav', {
        outlet: 'nav'
    });
},
model: function() {
    return this.store.find('user'); // Changed to user
}
});

这将返回一组用户,并且应该可以正常工作。

另请注意,您的用户模型错误。由于Users是一组用户,因此不需要包含所有用户属性。它应该是一个属性 - 用户。这个模型应该看起来:

App.Users = DS.Model.extend({
  user: DS.hasMany('user')
})