Why are all my Mongoose requests timing out? Why are all my Mongoose requests timing out? mongoose mongoose

Why are all my Mongoose requests timing out?


First you need to wait a connection to be established to make sure it will be ok, see Error handling:

try {  await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });} catch (error) {  handleError(error);}

Second you need to call mongoose.connection.close() after save call will be either resolved or rejected:

exampleOne.save().then(res => {  console.log(res)  mongoose.connection.close()})

because you didn't use await the save call didn't wait for resolve and mongoose.connection.close() was immediately called.

const res = await exampleOne.save()console.log(res)mongoose.connection.close()})


As I said in comment and also @Anatoly said you should send request (i.e. save) after that connection was established.

const mongoose = require('mongoose')const exampleSchema = new mongoose.Schema({  title: String,  author: String})const Example = mongoose.model('Example', exampleSchema)const mongoURL = //replace this comment with your own Mongo URLmongoose.connect(mongoURL, {   useNewUrlParser: true,   useUnifiedTopology: true,   useFindAndModify: false,   useCreateIndex: true }).then(() => {  const exampleOne = new Example({    title: 'Don Quixote',    author: 'M. Cervantes'  })  exampleOne.save().then(res => {    console.log(res)    mongoose.connection.close()  })}).catch(err => {  // handle error})