I’m new to MERN. I faced an issue while using MongoDB with Mongoose. Genuinely, I have no idea about it. An error like this:
const err = new MongooseError(message);
MongooseError: Operation fruits.insertOne()
buffering timed out after 10000ms
Here is my code:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/fruitsDB', {useNewUrlParser: true});
const fruitSchema = new mongoose.Schema({
name: String,
rating: Number,
review: String
});
const Fruit = mongoose.model('Fruit', fruitSchema);
const fruit = new Fruit({
name: "Banana",
rating: 7,
review: "Much loved."
});
fruit.save();
Full form of error in terminal:
C:Bekhruz's Coding JourneyBekhruz's ProgressMongooseAppnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:151
const err = new MongooseError(message);
^
MongooseError: Operation `fruits.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (C:Bekhruz's Coding JourneyBekhruz's ProgressMongooseAppnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:151:23)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)
Node.js v17.7.2
Thanks in advance.
3
Answers
Try adding
await
atmongoose.connect()
function. And handle errors withcatch
function as shown in this link.Make a top level async function to wait for the MongoDB to connect
Try/Catch your
save()
function.kindly check if you are connected with your DB.
This shall throw a proper error that can be used for debugging.