Question: I get the below error when I try to input 'node .app_servermodelsseed.cjs' into Windows Powershell: PS C:UsersquickOneDriveDocumentsGitHubCS-465-Full-Stack-Development-Itravlr> node .app_servermodelsseed.cjs C:UsersquickOneDriveDocumentsGitHubCS-465-Full-Stack-Development-Itravlr ode_modulesmongooselibdrivers ode-mongodb-nativecollection.js:187 const err =
I get the below error when I try to input 'node .\app_server\models\seed.cjs' into Windows Powershell:
PS C:\Users\quick\OneDrive\Documents\GitHub\CS-465-Full-Stack-Development-I\travlr> node .\app_server\models\seed.cjs C:\Users\quick\OneDrive\Documents\GitHub\CS-465-Full-Stack-Development-I\travlr ode_modules\mongoose\lib\drivers ode-mongodb-native\collection.js:187 const err = new MongooseError(message); ^
MongooseError: Operation `trips.deleteMany()` buffering timed out after 10000ms at Timeout.
Here is my code:
db.cjs
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 listner
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.cjs');
module.exports = mongoose;
seed.cjs
// Bring in the DB connection and the Trip schema
const mongoose = require('./db.cjs');
const Trip = require('./travlr.cjs');
// 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);
});
travlr.cjs
const mongoose = require('mongoose');
// Define the trip schema
const tripSchema = new mongoose.Schema({
code: { type: String, required: true, index: true, unique: true }, // Ensures trip codes are unique
name: { type: String, required: true, index: true },
length: { type: String, required: true },
start: { type: Date, required: true },
resort: { type: String, required: true },
perPerson: { type: Number, required: true }, // Changed to `Number` for numeric values
image: { type: String, required: true },
description: { type: String, required: true }
}, { timestamps: true }); // Adds `createdAt` and `updatedAt` fields automatically
// Create and export the Trip model
const Trip = mongoose.model('trips', tripSchema);
module.exports = Trip;
package.json
{
"name": "travlr",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "^4.21.2",
"hbs": "^4.2.0",
"http-errors": "~1.6.3",
"mongoose": "^8.15.1",
"morgan": "~1.9.1"
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
