Iam working on a api with node / express / mongoDB
Here is my Code for the api.js:
var express = require('express'),
mongoose = require('mongoose');
var app = module.exports = express();
mongoose.connect('correct login to mongolab');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'Datenbank Verbindungsfehler:'));
db.once('open', function() {
var plantSchema = new mongoose.Schema({
// TODO Schema vervollständigen
name: String,
desc: String
});
var illnessSchema = new mongoose.Schema({
name: String,
desc: String
});
var Plant = mongoose.model('plants', plantSchema);
var Illness = mongoose.model('illnesss', illnessSchema);
app.get('/api/find', function(req,res) {
Plant.find({name: new RegExp(req.query.input, "i")},function(err, query) {
res.json(query);
});
});
app.get('/api/plant', function(req,res) {
Plant.findOne({name: req.query.name},function(err, queryPlant) {
res.json(queryPlant);
});
});
app.get('/api/illness', function(req,res) {
Illness.find(function(err, queryIllness) {
res.json(queryIllness);
});
});
});
The plant schema is working totaly fine. The routings of the api give me back some nice json.
But the /api/illness route only returns: []
The Database:
it hast 2 Collections:
since the first works I will just give you a document of the second collection:
{
"_id" : ObjectId("543006eb31e79f8a8350422c"),
"name" : "test",
"desc" : "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum."
}
So there is a document that should be found by the /api/illness call, since he should pull out all of them.
I hope somebody can help me.
Mongoose pluralizes your model name to create the name of the collection, so because illnesss isn't a proper plural, a collection name of illnessses is likely being used.
To force Mongoose to use a specific collection name, pass it as the third parameter in your model call:
var Illness = mongoose.model('illnesss', illnessSchema, 'illnesss');