Search code examples
javascriptnode.jsmongodbmongoosedatabase-connection

MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms at Timeout. Error?


const mongoose = require("mongoose")

mongoose.connect("mongodb://localhost:27017/testdb")

const userSchema = new mongoose.Schema({
    name : String,
    age : Number
})

const User = mongoose.model("User", userSchema);

const user1 = new User({name: "Jack", age: 24});
user1.save().then(() => console.log("User Saved"));

I was trying to perform CRUD operations on mongoDB database using mongoose and this error popped up.


Solution

  • Establishing a connection is async operation.
    You need to await for it and only then you'll be able to run queries.

    await mongoose.connect("mongodb://localhost:27017/testdb")