Reputation:
I recently started working on a project with Express and since I'm using Node.js as backend I chose MongoDB as my database. It's my first time working with Mongo but I can't authenticate with Express, it works fine from terminal. I followed the guide from MogoDB blog here. I tried using their Atlas service where I had no problem authenticating. I'm using MongoDB driver. Here's how my connection URI looks like:
const uri = `mongodb://${username}:${password}@127.0.0.1/cloud?retryWrites=true&w=majority`;
I tried changing mongodb:// to mongodb+srv:// but that resulted in invalid connection string error.
Upvotes: 0
Views: 877
Reputation: 59456
You need to specify the authentication database, usually admin
:
const uri = `mongodb://${username}:${password}@127.0.0.1/cloud?authSource=admin&retryWrites=true&w=majority`;
If you don't specify it then in your case MonogDB defaults the authentication database to cloud
- which is most likely wrong.
Upvotes: 2
Reputation: 1
When you are accessing mongodb on the web you can click on connect and on connect your application. You will show the uri to copy paste starting with
mongodb+srv://USERNAME:PASSWORD@CLUSTER/DATABASE
You forgot to specify the CLUSTER. Currently is your local Database
Upvotes: 0