dimanche 4 janvier 2015

Ember Data breaks when fetching JSON from Node + Express REST server

I am trying to fetch JSON data from REST server built with Node.js and Express and then use it as a model in my Ember#Route.


The data I am trying to fetch:



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


The model I use:



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


I set up the RESTAdapter this way:



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


My Router looks like this:



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


I am aware that ember-data follows certain convention when it comes to JSON files. My server provides JSONs this way:



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


Then, after entering



http://localhost:8080/books


I get



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


but ember-data throws long error list that begins with:



"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
...


and now I don't know what is wrong and how to fix this.





Aucun commentaire:

Enregistrer un commentaire