Luiz E. Luiz E. - 3 months ago 25
JSON Question

Ember incorrectly deserialize json data

So I have a Spring Boot api returning a bunch of records in a list, like that:

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


but when I try to read it in the template, I get the following error

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


Here is my current adapter

import DS from 'ember-data';
import Ember from 'ember';

export default DS.RESTAdapter.extend({
host: 'http://127.0.0.1:8080/v1',
primaryKey: 'uid',
headers: Ember.computed('session.data.authenticated.currentUser.backendAccessToken', function() {
return {
"Auth-Token": this.get("session.data.authenticated.currentUser.backendAccessToken"),
};
})
});


what can I do in ember to allow it to read the data as I sent? I can change the API if needed, but I would rather not to

Answer

You need to use RESTSerializer. And primaryKey is for serializer not adapter. I think you use json serializer in your app.

UPDATE

Add below to your application serializer

normalizeSingleResponse(store, primaryModelClass, payload, id, requestType) {
        let typeKey = primaryModelClass.modelName;
        let ret = {};
        ret[typeKey] = payload;
        return this._normalizeResponse(store, primaryModelClass, ret, id, requestType, true);
    },

    normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
        let pluralTypeKey = Ember.Inflector.inflector.pluralize(primaryModelClass.modelName);
        let ret = {};
        ret[pluralTypeKey] = payload;
        return this._normalizeResponse(store, primaryModelClass, ret, id, requestType, false);
    }
Comments