0

I need to setup Mongo DB with my nodejs application . I created an account in mongoDb Atlas and I am trying to connect my app with the below URL with my username and password . Unfortunately I am getting authentication Fail error .

const MongoClient = require("mongodb").MongoClient;


const CONNECTION_URL = "mongodb+srv://<username>:<password>@renj0-2herp.mongodb.net/test?retryWrites=true";
const DATABASE_NAME = "example";

var app = Express();

app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));

var database, collection;

app.post("/person", (request, response) => {
    collection.insert(request.body, (error, result) => {
        if(error) {
            return response.status(500).send(error);
        }
        response.send(result.result);
    });
});

app.get("/people", (request, response) => {
    collection.find({}).toArray((error, result) => {
        if(error) {
            return response.status(500).send(error);
        }
        response.send(result);
    });
});

app.listen(3001, () => {
    MongoClient.connect(CONNECTION_URL, { useNewUrlParser: true }, (error, client) => {
        if(error) {
            throw error;
        }
        database = client.db(DATABASE_NAME);
        collection = database.collection("people");
        console.log("Connected to `" + DATABASE_NAME + "`!");
    });
});
7
  • 1
    Are you putting your username and password inside <> this angle bracket? Commented Mar 12, 2019 at 9:24
  • no .. without those Commented Mar 12, 2019 at 9:25
  • 1
    Ok, then please recheck your credentials Commented Mar 12, 2019 at 9:30
  • I have rechecked it many times .. IS it anything to do with ATLAS ? Commented Mar 12, 2019 at 9:37
  • Can you please show me the error? Commented Mar 12, 2019 at 9:38

2 Answers 2

2
const CONNECTION_URL = 'mongodb://username:password@localhost:27017/example'

Your connection url is wrong so i suggest you try this format or try below also

const CONNECTION_URL = 'mongodb://localhost:27017/example'
Sign up to request clarification or add additional context in comments.

Comments

1

you need to whitelist your IP adders with MongoDb Atlas. To do so, open cmd/terminal and run command "ipconfig" and search for "IPv4 Address" and copy the IP address. Now, open your Atlas cluster, goto security, and there you will find the option IP whitelist, click ADD IP ADDRESS and add you IP.

3 Comments

I chose connect any in the IP whitelist option so that any IP can connect
check you connection string (do not add "<" and ">" in user name and password). To find the connection string go to the Cluster Overview click Connect, select 2nd option connect to your application, Select Driver as NodeJS (By default) select Version, and below you will find connection string copy paste that and change user name and password. (do not add "<" and ">" in user name and password)
Ya I know that . I have entered just the username and password without < and >

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.