Reputation: 129
I have a MongoDB server which has a NodeJS server to interact with it. I want Ember Store to get data from this server. A tutorial has led me until this point but it does not work for me (tutorial), I think it is outdated but I am not sure. This part of the backend server is about sending data:
app.get('/api/appointments', function(req, res){
AppointmentModel.find({}, function(err, docs){
if(err) res.send({error:err});
else res.send({data:docs, "type":"appointment"});
});
});
A curl request proves that it does work:
curl http://localhost:4500/api/appointments
{"data":[{"flags":[],"dates":[],"invited":[],"_id":"5adc8b6724a69f4327c4c7af","author":7,"title":"Does this work"}],"type":"appointment"}
This is the part of the ember code that makes the request to the server:
export default Route.extend({
model: function(){
return this.store.findAll('appointment');
}
});
This is the error I get in the console of the webbrowser(chrome):
Error while processing route: index Assertion Failed: Encountered a resource object with an undefined type (resolved resource using (unknown mixin)) Error: Assertion Failed: Encountered a resource object with an undefined type (resolved resource using (unknown mixin))
at new EmberError (http://localhost:4200/assets/vendor.js:24475:25)
at Object.assert (http://localhost:4200/assets/vendor.js:24725:15)
at Class._normalizeResourceHelper (http://localhost:4200/assets/vendor.js:85052:61)
at Class._normalizeDocumentHelper (http://localhost:4200/assets/vendor.js:84993:25)
at Class._normalizeResponse (http://localhost:4200/assets/vendor.js:85121:36)
at Class.normalizeArrayResponse (http://localhost:4200/assets/vendor.js:86013:19)
at Class.normalizeFindAllResponse (http://localhost:4200/assets/vendor.js:85873:19)
at Class.normalizeResponse (http://localhost:4200/assets/vendor.js:85816:23)
at normalizeResponseHelper (http://localhost:4200/assets/vendor.js:76037:39)
at promise.then.adapterPayload (http://localhost:4200/assets/vendor.js:76889:19)
Because the error is something about types I checked and checked again the models but did not find anything that would give an error. Ember:
export default DS.Model.extend({
author: DS.attr('number'),
title: DS.attr('string'),
description: DS.attr('string'),
flags: DS.hasMany('number'),
dates: DS.hasMany('date'),
invited: DS.hasMany('number')
});
NodeJS/Mongo:
//The schema for appointments
var appointmentSchema = new mongoose.Schema({
author: Number, //Userid of the owner of the appointment.
title: String,
description: String,
flags: [Number], //Array of ids of flags that are set.
dates: [Date],
invited: [Number] //Array of user ids.
});
var AppointmentModel = mongoose.model('appointment', appointmentSchema);
In what special way does ember need the data in order to correcly handle it?
I have read some docs about store and ember but I am still new to the framework, any help is much appreciated.
Upvotes: 0
Views: 471
Reputation: 18240
First you need to understand ember datas model definitions. This does not make sense:
flags: DS.hasMany('number'),
this basically means *an appointments has multiple flags and they are of the model number
. you can not use hasMany
or belongsTo
for arrays or primitive types.
Now you have two options. Either you create models for flags, people (invited), etc, or you return simple JSON structures. For this specify attr()
(no argument given).
Your next problem is that by default ember assumes jsonapi
, but you're not sending jsonapi
. Here you can either move to jsonapi
on your server, or you modify your serializer as written here.
However with all this in mind it comes to my conclusion that you maybe dont want to use ember-data
at all. ember-data
is very useful for data that has relations and is based on models, but you can use ember
without it.
You just can just do this:
model() {
return $.getJSON('/api/appointments');
}
and then operate on the plain JSON you're sending.
If you want to use ember-data
or not depends totally on your use-case.
Upvotes: 2
Reputation: 347
From what i can see you're using the wrong adapter, your backend is using the JSONAPI specification http://jsonapi.org/, but ember is expecting REST, that's why the type property is failing, you just need to change the adapter from REST to JSONAPI, here you can find more info https://guides.emberjs.com/v3.0.0/models/customizing-adapters/ it should be pretty simple in your case just change the adapter to JSONAPIAdapter
Upvotes: 0