Command Error : ( MongoServerError: bad auth : Authentication failed )

Viewed 408

Really I am trying to do a mongoose connection from Mongo Atlas. I think that I have a cluster problem. Please check the code and give me an answer.

//db connection
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.qyrzo.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });

console.log('db connect');

1 Answers

you can use the mongoose library

you can try using a standard declaration


    mongoose.connect("mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.qyrzo.mongodb.net/myFirstDatabase?retryWrites=true&w=majority");

if you are on latest version of mongoose you don't need to add the optional parametes

{ useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 }

but you can add it makes no difference


    mongoose.connect("mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.qyrzo.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });

Related