Ember 从 Node + Express REST 服务器获取 JSON 时数据中断
Ember Data breaks when fetching JSON from Node + Express REST server
我正在尝试从使用 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 文件时遵循某些约定。
我的服务器以这种方式提供 JSONs:
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
...
现在我不知道出了什么问题以及如何解决这个问题。
看来我犯的错误是在声明模型时。 ID 属性不应该在这里声明,正确的模型是这样的:
App.Book = DS.Model.extend({
author: DS.attr('string'),
title: DS.attr('string')
});
我正在尝试从使用 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 文件时遵循某些约定。 我的服务器以这种方式提供 JSONs:
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
...
现在我不知道出了什么问题以及如何解决这个问题。
看来我犯的错误是在声明模型时。 ID 属性不应该在这里声明,正确的模型是这样的:
App.Book = DS.Model.extend({
author: DS.attr('string'),
title: DS.attr('string')
});