0

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");
})

12
  • Did you mean res.json(lists)? Commented Feb 27, 2020 at 11:01
  • no, i think res.send(lists) is correct. Commented Feb 27, 2020 at 11:05
  • Have you imported the list in app.js ? Have you checked your console errors ? Commented Feb 27, 2020 at 11:19
  • @Shl can you please check ? Commented Feb 27, 2020 at 11:29
  • @Juhil Somaiya yes it is , also the err not returning anything, is suspect its mongo issue , but dont know how to monitor it. Commented Feb 27, 2020 at 11:39

2 Answers 2

1

Try using this syntax

   app.get('/lists', (req,res) => {
     List.find({}).then( lists => {
        res.send(lists)
     }).catch(err =>
        console.log(err)
     );
   })
Sign up to request clarification or add additional context in comments.

2 Comments

Are you seeing List imported in app.js ? How it will get List ?
@JuhilSomaiya yes.
0

You are passing multiple argument in then block. Then is only accept single argument that is mark as success data. If you want to show errors also you have to pass in error argument in catch block.

Try This code :

    const express = require('express');
    const bodyParser = require('body-parser');
    const List = require('./listModel');
    const mongoConnection = require('./mongoose');
    const app = express();

app.use(bodyParser.json({
    limit: '20mb',
    extended: true
}));
app.use(bodyParser.urlencoded({
    extended: true
}));

app.get('/lists', (req,res) => {
     List.find().then( lists => {
                res.send(lists)
          }).catch(err =>
                res.send(err)
          );
 });

app.listen(3100, () => {
    console.log("Server is listening on port 3100");
})

9 Comments

I have tried out your code which is working fine with this solution.
There might be issue with his local mongodb @HardikPatel
it seem like it is a mongo issue indeed , any ideas how to verify/find issue
first of all make sure that you have mongoDB installed on your system as well as if installed then check that it is connected to your application.
i think you have not connected your mongoDB in your app. add following line in your app.js file const mongo = require('path to mongoconnection file');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.