Trouble getting a simple ember data working


#1

Hi all, i am getting the below message when i try to work on simple example using ember data. please help/suggest how to fix the problem.

WARNING: Encountered “0” in payload, but no model was found for model name “0” (resolved model name using test-emberdata@serializer:rotation-group:.modelNameFromPayloadKey(“0”))

below is the response from api.

[ { “groupIndex”:1, “creativeIds”:[ 1, 2 ] }, { “groupIndex”:2, “creativeIds”:[ 3, 4 ] } ]

code:

model: rotation-group.js.

import DS from ‘ember-data’;

export default DS.Model.extend({

 groupIndex: DS.attr('number'),
 creativeIds: DS.attr('array')

});

adapter: rotation-group.js

import Ember from ‘ember’;

import DS from ‘ember-data’;

export default DS.RESTAdapter.extend({

findAll: function(store, type, query, record) {
	let url = "http://localhost:3000/api/rotationGroups";
	return this.ajax(url, 'POST');
}

});

routes/test.js import Ember from ‘ember’;

export default Ember.Route.extend({

model: function(){
    return this.store.findAll('rotationGroup')
}

});

@rytis @square


#2

It’s happening because your backend is sending you rootless JSON


#3

can i do some thing from client side ?


#4

I tried to normalize like below, still the same problem

import Ember from ‘ember’;

import DS from ‘ember-data’;

export default DS.RESTSerializer.extend({

normalizeResponse: function(store, primaryModelClass, payload, id, requestType){
 payload.forEach((item, idx) => {
  item.id = 'rotationGroup'+idx;
 })
 payload = {
   id: 'rotationGroup',
   data: payload
 };
 return this._super(...arguments);
 }
});

#5

In your application serializer do :

export default DS.RESTSerializer.extend({
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);
    }
});

#6

@jmimi Hi thanks for reply, I tried i get the below error now. ember.debug.js:27730 Error while processing route: test Assertion Failed: Unable to find transform for ‘array’ Error: Assertion Failed: Unable to find transform for ‘array’


#7

Do you have array transformer?


#8

@jmimi

Works now with small change to your code, need to add id. Thank you so much for your help.

 normalizeSingleResponse(store, primaryModelClass, payload, id, requestType){
let typeKey = primaryModelClass.modelName;
let ret = {};
payload.forEach((item, idx) => {
  item.id = idx;
});
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 = {};
payload.forEach((item, idx) => {
  item.id = idx;
});
ret[pluralTypeKey] = payload;
return this._normalizeResponse(store, primaryModelClass, ret, id, requestType, false);

},