i was tryin to connect with mongodb. it shows me MongoServerError: bad auth : Authentication failed.
code given below
const express = require("express");
const cors = require("cors");
const { MongoClient } = require("mongodb");
require("dotenv").config();
const port = process.env.PORT || 4000;
const app = express();
app.use(cors());
app.use(express.json());
//mongodb connect
const uri = `mongodb+srv://${process.env.DATABASE_USER}:${process.env.DATABASE_PASS}@cluster0.zobm7.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
client.connect((err) => {
const collection = client.db("mac-yard").collection("products");
// perform actions on the collection object
console.log("database conected");
client.close();
});
app.get("/", (req, res) => {
res.send("hello");
});
app.listen(port, () => {
console.log("listening to port", +port);
});
3
Answers
I just encountered this error:
for my case user ID password were right .
but my error was "<>"
mongodb+srv://user:@cluster0
solution >>> No tag
mongodb+srv://user:password@cluster0
You probably already tried this already but I made a new user with the most basic login credentials and this stopped me from getting this error. See link to for support https://www.youtube.com/watch?v=wvlJGvP18Qk.
From there you can try making a user with your desired details.