Good morning everyone,
I’ve been working on a school project and I’m having trouble seeding the MongoDB database. When I run the provided code I get a “MongooseError: Operation ‘trips.deleteMany()’ buffering timed out after 10000ms”. I’ve ensured that my MongoDB is running, I can mongosh and manually manipulate the DB. I’ve also verified that the DB connects to my DBeaver app for a GUI so I’m not sure why Mongoose appears to be having issues connecting. Following other posts recommendations, I have also attempted to place ‘await’ commands before the connection attempts and before the deleteMany() attempts.
seed.js
// Brind in the DB connection and the Trip schema
const Mongoose = require('./db')
const Trip = require('./travlr');
// Read seed data from json file
var fs = require('fs');
var trips = JSON.parse(fs.readFileSync('./data/trips.json', 'utf8'))
// Delete any existing records, then insert seed data
const seedDB = async () => {
await Trip.deleteMany({});
await Trip.insertMany(trips);
};
// Close the MongoDB connection and exit
seedDB().then(async () => {
await Mongoose.connection.close();
process.exit(0);
});
db.js // This is the JavaScript file where the connection happens
const mongoose = require('mongoose');
const host = process.env.DB_HOST || '127.0.0.1';
const dbURI = 'mongodb://${host}/travlr';
const readLine = require('readline');
// Build the connection string and set the connection timeout.
// timeout is in milliseconds
const connect = () => {
setTimeout(() => mongoose.connect(dbURI, {
}), 1000);
}
// Monitor connection events
mongoose.connection.on('connected', () => {
console.log(`Mongoose connected to ${dbURI}`);
});
mongoose.connection.on('error', err => {
console.log('Mongoose connection error: ', err);
});
mongoose.connection.on('disconnected', () => {
console.log('Mongoose disconnected');
});
// Windows specific listener
if(process.platform === 'win32'){
const r1 = readLine.createInterface({
input: process.stdin,
output: process.stdout
});
r1.on('SIGINT', () => {
process.emit("SIGINT");
});
}
// Configure for Graceful Shutdown
const gracefulShutdown = (msg) => {
mongoose.connection.close(() => {
console.log(`Mongoose disconnected through ${msg}`);
});
};
//Event Listeners to process graceful shutdowns
// Shutdown invoked by nodemon signal
process.once('SIGUSR2', () => {
gracefulShutdown('nodemon restart');
process.kill(process.pid, 'SIGUSR2');
});
// Shutdown invoked by app termination
process.on('SIGINT', () => {
gracefulShutdown('app termination');
process.exit(0);
});
// Shutdown invoked by container termination
process.on('SIGTERM', () => {
gracefulShutdown('app shutdown');
process.exit(0);
});
// Make initial connection to DB
connect();
// Import Mongoose schema
require('./travlr');
module.exports = mongoose;
Any help would be appreciated.