Very simple Ember Data structure from JSON API

Good God!

I just want to set MODEL data from JSON API with Ember Data.

Let’s take some base example (list of user names):

URL: https://jsonplaceholder.typicode.com/users

My app/models/user file:

export default DS.Model.extend({
    name: DS.attr()
});

My app/adapters/application.js file:

export default DS.JSONAPIAdapter.extend({
  host: 'https://jsonplaceholder.typicode.com'
});

My app/router/users.js file:

export default Ember.Route.extend({
  model (){
    return this.store.findAll('user');
  }
});

My app/templates/users.hbs file:

{{#each model as |user|}}
  <li>{{user.name}}</li>
{{/each}}

What should I set to get a list of user names in MODEL and my template?

I just looked at this but as we almost always use ember-pouch and not JSONAPIAdapter I don’t know how to fix the error.

Assertion Failed: normalizeResponse must return a valid JSON API document: * One or more of the following keys must be present: “data”, “errors”, “meta”.

See Twiddle:

Thanks for your answer Broerse!

So the reason why the data can not be retrieved by JSONAPIAdapter is the missing data key in the JSON file?

I have to check and understand how the ember-pouch works.

I have created a jsonapi mock that works for 24 hours. Insert it in the Twiddle and it works https://demo5604487.mockable.io/

Content-Type application/vnd.api+json

{
  "data": [{
    "type": "users",
    "id": "1",
    "attributes": {
      "name": "John"
    }
  },{
    "type": "users",
    "id": "2",
    "attributes": {
      "name": "James"
    }      
  }]
}

(My pouch example GitHub - broerse/ember-cli-blog: Tom Dale's blog example updated for the Ember CLI)

1 Like

Updated my twiddle so this works for 24 hours.

1 Like

Thank you for your help! :slight_smile:

1 Like

The API you are trying to call does not follow the jsonapi.org specification.

In order to use ember-data out of the box, you need to use an api that follows the jsonapi specification.

Here are some examples.

http://jsonapi.org/implementations/#examples

2 Likes

You can also just start out with the RESTAdapter/RESTSerializer and adapt from there. For example, you could overwrite the handleResponse method to change the format the API returns into a format the default adapters can work with.

Hello,

i am facing same issue can you please update your twindle. it exipred

Hi @sk5202, this is a pretty old thread so you might have better luck just posting a new thread with some more details about what you’re seeing. An example server response and then any details about your adapter/serializer/model would probably be helpful.

I made a new mock that works for 24 hours. Ember Twiddle

We improved ember-pouch a lot since this old thread. We even started a free CouchDB hosting you can try with this on www.cloudstation.com

2 Likes

Thank you very much it solved my problem.

Problem is JSON adapter have fix format of JSON.

I didn’t pass from you JSON it was clear

1 Like