0
我嘗試構建一個將JSON數據返回到我的Ember.js應用程序的簡單API Express服務器。Ember.js:未定義的數據
這裏是我的服務器:
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');
在灰燼的一面,這是我的模型:
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
content: DS.attr('string'),
author: DS.attr('string'),
});
適配器:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
namespace: 'api',
host: 'http://localhost:4500'
});
串行器:
import DS from 'ember-data';
export default DS.JSONAPISerializer.extend({
primaryKey: '_id',
});
這是我的服務器響應,當我使用這個命令行: 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.}]}
最後,這是當我試圖讓這個模型是什麼灰燼迴應: Ember's respond
爲什麼我得到所有這些'未定義',以及如何解決這個問題?
非常感謝,它的工作原理! – TimothePearce