Issue with working on data from MongoDb
Connecting to MongoDb successfully ,
but on find command , it should return an empty collection , yet nothing is returned.
what could be the issue , or how it could be monitored by some error messaging.
Thanks.
Project dependencies
"dependencies": {
"body-parser": "^1.19.0",
"epxress": "0.0.1-security",
"express": "^4.17.1",
"nodemon": "^2.0.2"
}
mongoose.js
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/ListsDB', { useNewUrlParser: true, useUnifiedTopology: true}).then( ()=> {
console.log("Connected to MongoDbB successfully ");
}).catch( (e) => {
console.log("Error while attempting to connect to MongoDB");
console.log(e);
});
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
module.exports = {
mongoose
};
list.model.js
const mongoose = require('mongoose');
const ListSchema = new mongoose.Schema({
title: {
type: String,
required: true,
minlength: 1,
trim: true
}
})
const List = mongoose.model('List', ListSchema);
module.exports = { List }
app.js Not Working
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const { List, Task } = require('./db/models');
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next()
})
app.get('/lists', (req,res) => {
// We want to return an array of all the lists in the database
List.find({}).then( (err, lists)=> {
res.send(lists)
})
})
app.listen(3000, () => {
console.log("Server is listening on port 3000");
})
res.json(lists)?