javascript - Empty response in MEAN database query -
i trying node.js server communicate database can extend angular front end. have path makes request database should return entire database (5 documents). have connection database can see connect when refresh server.
i try list relevant code below:
loading modules...
var express = require('express'); var morgan = require('morgan'); var mongoose = require('mongoose'); var bodyparser = require('body-parser'); var methodoverride = require('method-override'); var app = express();
defining database schema , connecting
// database connection , scheme =================== mongoose.connect('mongodb://localhost/data/'); var schema = mongoose.schema({ date: 'string', newproofs: [number], changes: [number], output: [number] }); var test2014 = mongoose.model('test2014', schema);
middleware configurations. don't know methodoverride does. believe body parser allows response json?
// application middleware , configurations ============== app.set('view engine', 'ejs'); app.use(express.static(__dirname + '/public')); app.use(morgan('dev')); app.use(bodyparser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded app.use(bodyparser.json()); // parse application/json app.use(bodyparser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json json app.use(methodoverride());
path making request database.
app.get('/api/test2014', function(req, res) { // use mongoose weekdays display test2014.find(function(err, days) { // if there error retrieving, send error nothing else if (err) res.send(err); res.json(days); }); });
listening...
var server = app.listen(3000, function() { console.log('listening on port %d', server.address().port); });
when refresh page @ http://localhost:3000/api/test2014
see this:
{}
i expect see (what have in database).
> db.test2014.find().pretty() { "_id" : objectid("541c377d51b7f5ebbffb7ab1"), "date" : "09192014", "newproofs" : 15, "changes" : 16, "output" : 17 } { "_id" : objectid("541c4a4451b7f5ebbffb7ab2"), "date" : "09182014", "newproofs" : 7, "changes" : 8, "output" : 9 } { "_id" : objectid("541c4a5651b7f5ebbffb7ab3"), "date" : "09172014", "newproofs" : 8, "changes" : 9, "output" : 10 } { "_id" : objectid("541c4a7151b7f5ebbffb7ab4"), "date" : "09162014", "newproofs" : 9, "changes" : 10, "output" : 11 } { "_id" : objectid("541c4a8051b7f5ebbffb7ab5"), "date" : "09152014", "newproofs" : 10, "changes" : 11, "output" : 12 }
thanks reading!
ps - request header says accepts accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
does json need somewhere in there?
the problem @ following line:
var test2014 = mongoose.model('test2014', schema);
by default, when pass 2 arguments above function, mongoose assumes collection name being 'test2014s' first argument plus "s".
i see in query below collection name 'test2014'. recommend explicitly pass collection name , try following instead:
var test2014 = mongoose.model('test2014', schema, 'test2014');
Comments
Post a Comment