From b633bcf11a95c0a8ef4799a50e9913f5e50112f7 Mon Sep 17 00:00:00 2001 From: Sam12354 <161862599+Sam12354@users.noreply.github.com> Date: Sun, 5 Apr 2026 13:17:37 +0300 Subject: [PATCH 1/2] Add success log and improve error message in MongoDB connection and add const --- connection.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/connection.js b/connection.js index 85a9e12..7c93695 100644 --- a/connection.js +++ b/connection.js @@ -20,11 +20,15 @@ async function main() { // Connect to the MongoDB cluster await client.connect(); + // Log successful connection to MongoDB + console.log("Connected to MongoDB successfully"); + // Make the appropriate DB calls await listDatabases(client); } catch (e) { - console.error(e); + // Log a descriptive error message if connection or DB listing fails + console.error("Error connecting to MongoDB or listing databases:", e); } finally { // Close the connection to the MongoDB cluster await client.close(); @@ -38,7 +42,7 @@ main().catch(console.error); * @param {MongoClient} client A MongoClient that is connected to a cluster */ async function listDatabases(client) { - databasesList = await client.db().admin().listDatabases(); + const databasesList = await client.db().admin().listDatabases(); console.log("Databases:"); databasesList.databases.forEach(db => console.log(` - ${db.name}`)); From 548ece61e95cff470b761174648021bde34b444d Mon Sep 17 00:00:00 2001 From: Sam12354 <161862599+Sam12354@users.noreply.github.com> Date: Mon, 6 Apr 2026 12:37:43 +0300 Subject: [PATCH 2/2] Add error handling with catch block, fix formatting, and correct bathrooms field --- create.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/create.js b/create.js index d1d302c..9b689ee 100644 --- a/create.js +++ b/create.js @@ -46,7 +46,7 @@ async function main(){ name: "Private room in London", property_type: "Apartment", bedrooms: 1, - bathroom: 1 + bathrooms: 1 }, { name: "Beautiful Beach House", @@ -57,6 +57,9 @@ async function main(){ last_review: new Date() } ]); + } catch (e) { + // Handle any errors during database operations + console.error("Error while creating listings:", e); } finally { // Close the connection to the MongoDB cluster await client.close();