This commit is contained in:
2025-11-04 19:32:58 +03:00
parent 71f3f353ab
commit 69eddf47db
2 changed files with 9 additions and 4 deletions

View File

@@ -39,7 +39,9 @@ const connectWithUri = async (uri, label) => {
}); });
try { try {
await connection.connection.db.admin().command({ ping: 1 }); if (connection?.connection?.db) {
await connection.connection.db.admin().command({ ping: 1 });
}
} catch (pingError) { } catch (pingError) {
if (isAuthError(pingError)) { if (isAuthError(pingError)) {
await mongoose.connection.close().catch(() => {}); await mongoose.connection.close().catch(() => {});
@@ -49,10 +51,10 @@ const connectWithUri = async (uri, label) => {
} }
console.log('✅ MongoDB подключена успешно!'); console.log('✅ MongoDB подключена успешно!');
console.log(` Хост: ${connection.connection.host}`); console.log(` Хост: ${connection?.connection?.host || 'не указан'}`);
console.log(` БД: ${connection.connection.name}\n`); console.log(` БД: ${connection?.connection?.name || 'не указана'}\n`);
if (process.env.DEV === 'true') { if (process.env.DEV === 'true') {
console.log(` Пользователь: ${connection.connection.user || 'anonymous'}`); console.log(` Пользователь: ${connection?.connection?.user || 'anonymous'}`);
} }
return connection; return connection;

View File

@@ -117,6 +117,9 @@ const waitForDatabaseConnection = async () => {
const verifyAuth = async () => { const verifyAuth = async () => {
try { try {
if (!mongoose.connection.db) {
return false;
}
await mongoose.connection.db.admin().command({ listDatabases: 1 }); await mongoose.connection.db.admin().command({ listDatabases: 1 });
return true; return true;
} catch (error) { } catch (error) {