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}`)); 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();