I am trying to get my Node.js server to communicate with a database so that I can extend it to an Angular front end. I have a path that makes a request to the database that should return the entire database (5 documents). I have a connection to the database as I can see it connect when I refresh the server.
I will try to list only the relevant code below:
Loading the 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 the database schema and connecting
// Database connection and 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. I don't know what methodOverride does. I believe the body parser allows the response to be JSON?
// Application middleware and 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 as json
app.use(methodOverride());
Path for making request to database.
app.get('/api/TEST2014', function(req, res) {
// Use mongoose to get all weekdays for display
TEST2014.find(function(err, days) {
// if there is an error retrieving, send the error then 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 I refresh the page at http://localhost:3000/api/TEST2014 I see this:
{}
I expect to see this (what I currently have in the 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 for reading!
PS - My request header says it accepts Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Does JSON need to be somewhere in there?
The problem is at the following line:
var TEST2014 = mongoose.model('TEST2014', schema);
By default, when you pass only 2 arguments to the above function, mongoose assumes the collection name as being 'TEST2014s' that is first argument plus an "s".
I see in your query below that your collection name is 'TEST2014'. I would recommend you to explicitly pass the collection name and try the following instead:
var TEST2014 = mongoose.model('TEST2014', schema, 'TEST2014');
I was able to remedy the problem by changing the mongoose connection to the following:
mongodb://<local ip>:27017/<database name>
Closing this question.