I’ve been struggling with getting my cluster to connect in various node.js applications. Here’s the following troubleshooting techniques I’ve tried:
Database access - I’ve confirmed that my password for ‘user1’ is typed in correctly in the below connection URL.
Network access - 0.0.0.0/0 is on my list of IP Addresses in the IP Access List. It says in parentheses that this includes my current IP address.
Here’s the code copied from Clusters → Connect → MongoDB Driver connection code sample on the mongoDB website (my cluster’s name is Cluster0):
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://user1:<password>@cluster0.1umconu.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0";
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Send a ping to confirm a successful connection
await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);