TLDR: Swift developer here, haven't used Node in half or so, trying to set up an Express app that serves JSON from mongodb but instead of valid JSON I get an empty array. Help me /wdg/...
Model:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const schema = new Schema({
date: String
}, {
collection: 'importantDates'
});
schema.set("collection", "importantDates");
module.exports = mongoose.model("date", schema);
Router:
const express = require('express');
const router = express.Router();
const Date = require('../models/date');
router.route("/dates").get((req, res) => {
Date.find((err, date) => {
if(err) {
res.send(err);
} else {
res.json(date);
console.log(date);
}
})
});
module.exports = router;
App/server:
const express = require('express');
const app = express();
let json = require('./users.json');
const mongoose = require('mongoose');
const db = require('./db/config');
mongoose.connect(db.url);
const router = require('./router/router');
let databaseConnection = mongoose.connection;
const bodyParser = require('body-parser');
app.get('/listUsers', (req, res) => {
res.json(json)
});
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use('/', router);
databaseConnection.once('open', () => {
console.log("Connected to Mongodb");
});
const server = app.listen(3000, () => {
let host = '127.0.0.1';
let port = process.env.PORT || 3000
console.log(`app listening on ${host}:${port}`);
});
This code used to work before, now it doesn't. The name of my one and only collection is 'importantDates', haven't touched javascript and node for a while. Roodypoo...