Ember incorrectly deserializing data from API


#1

Hello everyone! So, I’ve struggling with something that many people also been through, but I couldn’t find any solution.

I have an API returning data as follows:

[
  {
    "createdAt": 1391708660000,
    "uid": 16587783,
    "name": "FRaaS",
    "fullName": "caarlos0/FRaaS",
    "description": "Fake RT as a Service",
    "homepage": "http://fraas.herokuapp.com/",
    "owner": "caarlos0",
    "organization": null,
    "joined": false,
    "private": false
  },
.... keeps going on

basically, it is a list of Github repos. I want to show them in my app, so I created a new model and the routes, etc

import DS from 'ember-data';

export default DS.Model.extend({
  uid: DS.attr('integer'),
  name: DS.attr('string'),
  fullName: DS.attr('string'),
  description: DS.attr('string'),
  homepage: DS.attr('string'),
  owner: DS.attr('string'),
  private: DS.attr('boolean')
});

and loading them in the route

export default Ember.Route.extend(AuthenticatedRouteMixin,{
  session: Ember.inject.service('session'),
  model(){
    return this.get('store').findAll('room');
  },

but for some reason, I keep seeing this message

WARNING: Encountered "0" in payload, but no model was found for model name "0" (resolved model name using chathub-ember@serializer:application:.modelNameFromPayloadKey("0"))

and I see like 100 times this in the console, just changing the “0” to “1” and so on… AFAIK, Embers expects a json with a root, and the api returns without a root. what can I do?

I’m using ember 2.7.0


#2

You’re able to transform your payload into what Ember Data expects. I recommended checking out this guide, https://guides.emberjs.com/v2.7.0/models/customizing-serializers/

That said, the API response looks a lot like what the DS.JSONSerializer expects. Does the below snippet fix the issue?

// app/serializers/room.js
import DS from 'ember-data';

export default DS.JSONSerializer.extend({});

#3

Worked as expected, although I had to change the API to return the id of the relations instead the full object