1

I am working with Ember and attempting to retrieve data from my REST API, but it is not working as intended. First of all i am quite unsure how to debug RESTAdapters and second of all i don't see the call being made in the Chrome Devtools Network view.

HTML:

<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.min.js"></script>
<script src="http://builds.emberjs.com/release/ember.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ember-data.js/1.0.0-beta.11/ember-data.js"></script>

Here is my JS:

App = Ember.Application.create();

App.Store = DS.Store.extend({
    adapter: DS.RESTAdapter.extend({
        host: 'http://example.com/api'
    })
});

App.Bodypart = DS.Model.extend({
    name: DS.attr('string')
});

App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return App.Bodypart.find();
    }
});

My console returns:

Error while processing route: index undefined is not a function TypeError: undefined is not a function

I understand this means that i obviously am returning some null pointer or a empty model.

So the question arrises, how do i debug the RESTAdapter?

MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
JavaCake
  • 4,075
  • 14
  • 62
  • 125

2 Answers2

3

You seem to be using an old ember-data syntax and applying it to 1.0beta11. I don't have a link at the moment, but check on the official repository or the guides. The model hook in your route should change from:

return App.Bodypart.find();

to

return this.store.find('bodypart');

Also you seem to be declaring your adapter incorrectly as well. It should be:

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

Also, consider changing the request from the ApplicationRoute to a child route, or you may experience "fake frozen" application as shown here

Community
  • 1
  • 1
MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
  • Thanks, it seems to be working, but i think theres a problem with the JSON response from my API, as it returns like this `[{"id":1,"name":"Something"},{"id":2,"name":"Something else"}]` which does not incorporate the `Bodypart` variable. The error i get now is: `Error while processing route: index Assertion Failed: The response from a findAll must be an Array, not undefined Error: Assertion Failed: The response from a findAll must be an Array, not undefined` – JavaCake Nov 14 '14 at 18:16
  • 1
    your json should be `{bodyparts: [{...}, {...}]}`. Note that the root container has to be in the plural for arrays and singular for single item: `{bodypart: {...} }` – MilkyWayJoe Nov 14 '14 at 19:16
  • Makes pretty good sense. Thanks for the tip concerning the router, i will get that fixed. – JavaCake Nov 14 '14 at 19:31
1

Regarding debugging Ember, checking the tag wiki: https://stackoverflow.com/tags/ember.js/info shows the following link: Debugging Ember

That may help.

Community
  • 1
  • 1