'Operation `kittens.insertOne()` buffering timed out after 10000ms

I am new guy and I am trying to make schema and trying to add new data to the schema and I have tried every possible solutions but that didn't work out for me..

Here is my app.js

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/db_name',
() => {
console.log("Connected")
});

const kittySchema = new mongoose.Schema({
name: String
});
kittySchema.methods.speak = function speak() {
var greeting = "My name is " + this.name

console.log(greeting);
};

const Kitten = mongoose.model('Kitten', kittySchema);
var akitty = new Kitten({ name: 'Akashkitty' });

akitty.save();
akitty.speak();

ERROR

C:\Backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:151
      const err = new MongooseError(message);
                  ^

MongooseError: Operation `kittens.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (C:\Backend\node_modules\mongoose\lib\drivers\node-mongodb- 
native\collection.js:151:23)       
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)


Solution 1:[1]

I have run your code and got the desired result as it should print. The result I have got is below - enter image description here

I think you should delete the node_modules and reinstall mongoose and other npm directories with npm i and that should solve your issue I hope so.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abu Sayad Hussain