dimanche 31 juillet 2016

Ember.js : Undefined data

I try to build a simple API Express server who return JSON data to my Ember.js App.

Here is my server :

var express = require('express');
var mongoose = require('mongoose');

var app = express();

mongoose.connect('mongodb://localhost/dataTest2');

var noteSchema = new mongoose.Schema({
    _id         : String,
    title       : String,
    content : String,
});

var NoteModel = mongoose.model('note',noteSchema);


app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
    next();
});

app.get('/api/notes', (req,res) => {
    NoteModel.find({},(err,data) => {
        if(err) {
            res.send({error:err});
        }
        else {
            res.setHeader('Content-Type', 'application/json');
            res.json({data:data});
        }
    });
});

app.listen('4500');

On the Ember side, this is my model :

import DS from 'ember-data';

export default DS.Model.extend({
    title: DS.attr('string'),
    content: DS.attr('string'),
    author: DS.attr('string'),
});

The adapter :

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
    namespace: 'api',
    host: 'http://localhost:4500'
});

The serializer :

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
    primaryKey: '_id',
});

This is what my server respond when I use this command line : curl http://localhost:4500/api/notes :

{"data":[{"_id":"579e2f68dfd5b298c9a3d732","title":"Eyeris","content":"Quis enim labore","type":"note"},
{"_id":"579e2f68dfd5b298c9a3d733","title":"Mazuda","content":"Voluptate cupidatat irure ,"type":"note"},
{etc.}]}

And finally, this is what Ember respond when I try to get this model : Ember's respond

Why do I get all these 'undefined', and how to solve this problem ?




Aucun commentaire:

Enregistrer un commentaire