Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.92k stars 3.84k forks source link

Mongoose error : buffering timed out after 10000ms #9732

Closed AntoineIN closed 3 years ago

AntoineIN commented 3 years ago

Hello, i got this error on mongoose with nodejs :

version of mongoose : 5.11.8

I'm trying to save data on my mongodb database via mongoose. But i got this error everytime : MongooseError: Operationusermodels.insertOne()buffering timed out after 10000ms

I'm sure that my server is connected to the database.

This is my code :

const MongoClient = require('mongodb').MongoClient;
const mongoose = require('mongoose');

const uri = `link for my database`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

client.connect()
    .then(() => {
        console.log('start');
    })
    .catch(err => {
        console.error('App starting error:', err.stack);
        process.exit(1)
    });

const Schema = mongoose.Schema;

const UserSchema = new Schema({
    user: String,
    usermail: String,
    userpwd: String,
    updated: { type: Date, default: Date.now },
});

const user_instance = mongoose.model('UserModel', UserSchema );

const myUser = new user_instance({ 
    user: "React JS",
    usermail: "test@test.com",
    userpwd: "teest",
});

myUser.save(function (err) { 
    console.log(err) 
});

Any idea ? Thanks you !

ParasiteTM commented 3 years ago

Check if you have the correct network access to your cluster.

kkhanhluu commented 3 years ago

You are using mongo client to connect to database. Try to connect to mongodb database by using mongoose.connect() or mongoose.createConnection()

xuke-hat commented 3 years ago

I faced the same issue. My mongoose's version is 5.11.8 and node's version is 14.15.1.

I'm following a tutorial of using MongoDb Atlas. I runned the following code and the connection was fine. But when I tried to insert something it showed MongooseError: Operation `notes.insertOne()` buffering timed out after 10000ms

const url = `mongodb+srv://notes:xxx@cluster0.fwxrn.mongodb.net/note-app?retryWrites=true&w=majority`

const mongoose = require('mongoose')
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })

const noteSchema = new mongoose.Schema({
  content: String,
  date: Date,
  important: Boolean,
})

const Note = mongoose.model('Note', noteSchema)

const note = new Note({
  content: 'HTML is Easy',
  date: new Date(),
  important: true,
})

note.save().then(result => {
  console.log('note saved!')
  mongoose.connection.close()
})
kkhanhluu commented 3 years ago

@xuke-hat is your authentication data correct? did you try to remove node_modules folder and reinstall mongoose package? This answer can be helpful

xuke-hat commented 3 years ago

@kkhanhluu The answer didn't work but thanks for your help.

Now I've solved the error. I thought the timeout in insertion implied that the connection was fine, but I'm wrong. It showed that I failed to connect to the database, though I checked my password many times and the IP whitelist is 0.0.0.0/0. Finally, I followed this answer and changed my DNS to 8.8.8.8 and it works.

vkarpov15 commented 3 years ago

@AntoineIN your script isn't actually using Mongoose to connect. You're using the MongoDB driver's MongoClient class, but then you aren't actually telling Mongoose to connect to MongoDB. That's why you're getting this error.

Replace

const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

client.connect().then(...)

With:

mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true }).then(...)

Please read the connection docs.

Ding-Fan commented 3 years ago

In my situation, I followed this answer https://stackoverflow.com/a/63912290/6236633 and made it working

gugateider commented 3 years ago

The problem I faced was that I was able to connect to my MongoDB Atlas through Compass but wasn't able to connect through my app. Had exactly the same issue that you've had, tried all the solutions above but none of them worked.

The problem was that apparently, my DNS was being cached and once I restarted my router everything started working again. I'm not sure if your problem is the same as mine but make sure you have whitelisted your IP and try restarting your router

shawndewet commented 3 years ago

I had to reboot my dev machine for this error to go away!