-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestConnection.js
28 lines (23 loc) · 1.07 KB
/
testConnection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Import the default connectToDatabase function from the db module
const connectToDatabase = require("./db").default;
// Async function to test the MongoDB connection
async function testMongoConnection() {
try {
// Attempt to connect to the database
const db = await connectToDatabase();
// Access the 'recipes' collection from the database
const collection = db.collection("recipes"); // You can change the collection name as needed
// Fetch a document from the collection where the title is "Biryani"
const document = await collection.findOne({ title: "Biryani" }); // This serves as a test to verify the connection
// Log the document found to the console
console.log("Document found:", document);
} catch (error) {
// Log any errors encountered during the fetch operation
console.error("Error fetching data:", error);
} finally {
// Exit the process when done to prevent hanging connections
process.exit(0); // Exits the Node.js process with success
}
}
// Call the test function to execute the connection test
testMongoConnection();