0

I am new in NodeJS. There is no error while connecting to mongodb

app.js

const express = require('express');
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');

require('dotenv/config')

app.use(bodyParser.json()); 
const postsRoute = require('./routes/posts');
app.use('/posts', postsRoute);


app.get('/',(req, res)=>{
    res.send("Hello");
});

//connect to db 
mongoose.connect(process.env.DB_CONNECT,{ useNewUrlParser: true },()=>{
    console.log("connected to db");
});

app.listen(3000);

post.js

const express = require('express');
const Post = require('../models/Post');
const router = express.Router();

router.get('/',(req, res)=>{
    res.send('we are on posts');
});
router.post('/',(req, res)=>{
    const post = new Post({
        title: req.body.title,
        description: req.body.description
    });
    
    post.save()
    .then(data =>{
        res.json(data);
    })
    .catch(err =>{
        console.log(err)
    }); 
});

module.exports = router;

in console:

connected to db

MongooseError: Operation posts.insertOne() buffering timed out after 10000ms at Timeout. (D:\projects\PracticeNode\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:197:23)

Muskan
  • 1
  • 1
  • This error generally means that your code is not able to reach your Mongo DB. Are you running your Mongo DB locally or is it deployed somewhere remotely? If remove, have you verified that the URI is correct and that the remote Mongo is reachable? – thanatonian2311 Aug 10 '21 at 07:56
  • This post has a lot of helpful answers - https://stackoverflow.com/questions/65408618/mongooseerror-operation-users-findone-buffering-timed-out-after-10000ms?rq=1 – thanatonian2311 Aug 10 '21 at 07:58
  • For mongoose.connect(), it is working fine, and it running locally. – Muskan Aug 11 '21 at 16:25

0 Answers0