Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}`));
Expand Down
5 changes: 4 additions & 1 deletion create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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();
Expand Down