0

As I mentioned in the title, sorry because this is a duplicate question, but literally any of given possible solutions doesn't work. To be specific, I created .env file which constists of 2 variables, username and password (which are from created projects, not from mongodb login page):

const uri = `mongodb+srv://${mongo_username}:${mongo_password}@cluster0.whz2jow.mongodb.net/?retryWrites=true&w=majority`;
const mongo_username = process.env["MONGO_USERNAME"];
const mongo_password = process.env["MONGO_PASSWORD"];

When I try console.log that two it really works. But then when I want to connect:

MongoClient.connect(uri, {
  maxPoolSize: 50, 
  //wtimeoutMS: 2500, 
  useNewUrlParser: true,
  useUnifiedTopology: true,
})
  .catch((err) => {
    console.error(err.stack); 
    process.exit(1); 
  })
  .then(async (client) => {
   
    app.listen(port, () => {
      console.log(`listening on port ${port}`);
    });
  });

problem happend here somewhere but for 2 days I cannot figure out why... If anyone has an idea how I can solve this, thanks in advance.

  • See https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Feb 24 '23 at 18:37
  • Do either of your `mongo_username` or `mongo_password` need to be URI encoded? – rickhg12hs Feb 25 '23 at 00:48
  • No, and actually I figured out what the problem was. In my .env file when I declared username and password at the end of variable I put ; but it should be without it. Now it works. – user18126722 Feb 25 '23 at 22:51

0 Answers0