1
votes

I'm making an End-of-degree project of a city dashboard. I'm usin: - PostgreSQL as DB - Node.js + Express.js + Massive.js as server - Ember.js as client app

Actually, I'm testing if I can get the data from DB to Ember, but I'm getting the next error (I've tried almost every solution I found here):

Error while processing route: index Assertion Failed: You must include an 'id' for poblacio in an object passed to 'push' Error: Assertion Failed: You must include an 'id' for poblacio in an object passed to 'push'

These are my files:

server.js (just a piece)

router.route('/poblacios')
        .get(function(request, response) {
            db.poblacio.find({}, function(err, res){
                response.json(res);
            });
        });

 app.use('/api/v1/', router);

adapters/application.js

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
    namespace: 'api/v1'
});

route/application.js

import Ember from 'ember';

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

models/poblacio.js

import DS from 'ember-data';

export default DS.Model.extend({
  anny: DS.attr('number'),
  districte: DS.attr('number'),
  barri: DS.attr('string')
});

serializers/poblacio.js

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
    primaryKey: 'id',
    normalizeFindAllResponse(store, type, payload) {
        return {
            data: {
                id: payload.id,
                type: type.modelName,
                attributes: {
                    anny: payload.anny,
                    districte: payload.districte,
                    barri: payload.barri,
                }               
            }
        };
    }
});

localhost:3000/api/v1/poblacios returns the number of women and men from 0 to 95+ years by district, neighbourhood and year, with an id for each row:

[{"id":1,"anny":2015,"districte":1,"barri":"1. el Raval","donesanys0":206,"donesanys1":212,"donesanys2":206,"donesanys3":247....
{"id":2,"anny":2015,"districte":1,"barri":"2. el Barri Gotic","donesanys0":48,"donesanys1":53...
....
{"id":657,"anny":2007,"districte":10,"barri":"73. la Verneda i la Pau","donesanys0":103,"donesanys1":118,"donesanys2":123,"donesanys3":107...

Thanks for your help!

1

1 Answers

1
votes

If you use the JSONAPIAdapter you need to follow the JSON API spec.

The JSON above, according to the spec, should be something similar to:

{
  data: [
    ... collection of resource objects
  ]
}

And that is the problem, your payload is an array, therefore a valid solution would be on the line of:

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
    primaryKey: 'id',
    normalizeFindAllResponse(store, type, payload) {
        return {
            data: payload.map((el) => {
              return {
                id: el.id,
                type: type.modelName,
                attributes: { /* the attributes you need from el */ }
              }
            })
        };
    }
});

Hope this helps.