when I run my app with npm run seeder
then I have face this error
I have checked my database connection carefully, it's ok.
also, I have checked my ordermodels file it's also ok. I have used MongoDB compass there is nothing problem. I don't know why showing buffering timed out.
MongooseError: Operation `orders.deleteMany()` buffering timed out after 10000ms
seeder.js
import mongoose from "mongoose";
import dotenv from "dotenv";
import colors from "colors";
import users from "./data/users.js";
import products from "./data/products.js";
import User from "./models/userModel.js";
import Product from "./models/productModel.js";
import Order from "./models/orderModel.js";
import connectDB from "./config/db.js";
dotenv.config();
connectDB();
const importData = async () => {
try {
await Order.deleteMany();
await Product.deleteMany();
await User.deleteMany();
const createUsers = await User.insertMany(users);
const adminUser = createUsers[0]._id;
const sampleProducts = products.map((product) => {
return { ...product, user: adminUser };
});
await Product.insertMany(sampleProducts);
console.log("Data Imported".green.inverse);
process.exit();
} catch (error) {
console.error(`${error}`.red.inverse);
process.exit(1);
}
};
const DeleteData = async () => {
try {
await Order.deleteMany();
await Product.deleteMany();
await User.deleteMany();
console.log("Data Deleted".red.inverse);
process.exit();
} catch (error) {
console.error(`${error}`.red.inverse);
process.exit(1);
}
};
if (process.argv[2] === "-d") {
DeleteData();
} else {
importData();
}
I have the same issue and I just did a research and I find that your MongoDB are trying to execute the function User.deleteMany() before the database is connected.
just put an await before connectDB();
await connectDB();
use following code for connect to mongodb
const mongoose = require('mongoose')
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true })
after that you should removing the node_module folder and all .json files and reinstalling the mongoose and use npm update
I have faced the same issue, but I managed to resolve it.
So in package.json, in the scripts section I've added:
"data:import": "node backend/seeder",
"data:destroy": "node backend/seeder -d",
Then:
npm run data:import
After trying a couple of times, I've got Data Imported message in the console.
Note: I haven't uninstalled any node_modules.