【问题标题】:Ember Data breaks when fetching JSON from Node + Express REST server从 Node + Express REST 服务器获取 JSON 时,Ember 数据中断
【发布时间】:2015-01-04 14:49:59
【问题描述】:

我正在尝试从使用 Node.js 和 Express 构建的 REST 服务器获取 JSON 数据,然后将其用作我的 Ember#Route 中的模型。

我要获取的数据:

var books = [
    { id: 98, author: 'Stanisław Lem', title: 'Solaris' },
    { id: 99, author: 'Andrzej Sapkowski', title: 'Wiedźmin' }
];

我使用的模型:

App.Book = DS.Model.extend({
    id: DS.attr('number'),
    author: DS.attr('string'),
    title: DS.attr('string')
});

我是这样设置 RESTAdapter 的:

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

映射:

App.Router.map(function () {
    this.resource("books");
});

我的路线如下所示:

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

我知道 ember-data 在涉及 JSON 文件时遵循一定的约定。 我的服务器以这种方式提供 JSON:

app.get('/books', function (request, response) {
    console.log('In GET function ');
    response.json({'books': books})
});

然后,进入后

http://localhost:8080/books

我明白了

{"books":[{"id":98,"author":"Stanisław Lem","title":"Solaris"},{"id":99,"author":"Andrzej Sapkowski","title":"Wiedźmin"}]}

但是当我进入时

http://localhost:8080/#/books

ember-data 抛出以:

开头的长错误列表
"Error while processing route: books" "invalid 'in' operand record._attributes"    
"ember$data$lib$system$model$attributes$$getValue@http://localhost:8080/static/ember-data.js:8176:1
ember$data$lib$system$model$attributes$$attr/<@http://localhost:8080/static/ember-data.js:8202:26
computedPropertySet@http://localhost:8080/static/ember.prod.js:11882:15
computedPropertySetWithSuspend@http://localhost:8080/static/ember.prod.js:11842:9
makeCtor/Class@http://localhost:8080/static/ember.prod.js:33887:17
...

现在我不知道出了什么问题以及如何解决这个问题。

【问题讨论】:

    标签: node.js rest ember.js express ember-data


    【解决方案1】:

    看起来我犯的错误是在声明模型时。 ID 属性不应在此处声明,正确的模型如下所示:

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

    【讨论】:

      猜你喜欢
      • 2016-09-13
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 2015-05-27
      • 2017-11-20
      • 2014-04-24
      • 2013-07-10
      • 2010-09-27
      相关资源
      最近更新 更多