Deploy Bot
Fix: Add DB Self-Healing and CastError Validation
bfde415
const mongoose = require('mongoose');
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URI);
console.log('✅ MongoDB ulandi');
// SELF-HEALING: Drop obsolete index 'orderId_1' if it exists to prevent E11000 error
try {
const collection = mongoose.connection.collection('orders');
const indexes = await collection.indexes();
const obsoleteIndex = indexes.find(idx => idx.name === 'orderId_1');
if (obsoleteIndex) {
console.log("⚠️ Found obsolete index 'orderId_1'. Dropping...");
await collection.dropIndex('orderId_1');
console.log("✅ Obsolete index 'orderId_1' dropped successfully.");
}
} catch (dbErr) {
console.error("⚠️ Index Check Error (Non-fatal):", dbErr.message);
}
} catch (err) {
console.error('❌ MongoDB ulanishda xatolik:', err);
process.exit(1);
}
};
module.exports = connectDB;