'MongoServerError: bad auth : Authentication failed

why do I get this kind of error? when I send a res to MongoDB that time it is showing me this kind of error what should I do Now?



Solution 1:[1]

You can find the same question here I just encountered this error:

    Server is running on port: 5000
MongoServerError: bad auth : Authentication failed.
    at Connection.onMessage (/home/mostafa/documents/local-repositories/mern-mongodb-tutorial/server/node_modules/mongodb/lib/cmap/connection.js:202:30)
    at MessageStream.<anonymous> (/home/mostafa/documents/local-repositories/mern-mongodb-tutorial/server/node_modules/mongodb/lib/cmap/connection.js:62:60)
    at MessageStream.emit (node:events:527:28)
    at processIncomingData (/home/mostafa/documents/local-repositories/mern-mongodb-tutorial/server/node_modules/mongodb/lib/cmap/message_stream.js:108:16)
    at MessageStream._write (/home/mostafa/documents/local-repositories/mern-mongodb-tutorial/server/node_modules/mongodb/lib/cmap/message_stream.js:28:9)
    at writeOrBuffer (node:internal/streams/writable:390:12)
    at _write (node:internal/streams/writable:331:10)
    at Writable.write (node:internal/streams/writable:335:10)
    at TLSSocket.ondata (node:internal/streams/readable:766:22)
    at TLSSocket.emit (node:events:527:28) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError',
  [Symbol(errorLabels)]: Set(1) { 'HandshakeError' }

Solution: If you've got a config.env file in your project server side directories, where you have put your ATLAS_URI Just login to your https://cloud.mongodb.com account and beside your Cluster name, you would find a green button named Connect, there, click on Connect your application and copy the connection string. Go back to the config.env file and update the ATLAS_URI like below: ATLAS_URI=mongodb+srv://<username>:<password>@sandbox.jadwj.mongodb.net/employees?retryWrites=true&w=majority Now I think you are good to go ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anonymoustafa