Home > Software design >  NodeJS to Mongo DB Atlas not connecting
NodeJS to Mongo DB Atlas not connecting

Time:08-03

I'm new to NodeJS and am having difficulties connecting to Mongo DB atlas.

app.js

const mongoose = require("mongoose");
const express = require("express");
const app = express();
require("dotenv").config();

mongoose.connect(process.env.DATABASE, {
          useNewUrlParser: true,
          useUnifiedTopology: true,
          useCreateIndex:true,
}).then(() => {
          console.log("DB CONNECTED")
}).catch(() => {
          console.log("UNABLE TO CONNECT TO DB")
});

process.env

DATABASE = mongodb srv://<DATABASE_NAME>:<DATABASE_PASSWORD>@cluster0.mzive.mongodb.net/? 
retryWrites=true&w=majority

Edit: I did add the database name and password in the slots <DATABASE_NAME> and <DATABASE_PASSWORD> I just swapped it out for the post.

CodePudding user response:

You have to write your name, password, and database in the URL

eg, if your database name is krishna and password is dev, and database name is a store

then URL will be

mongodb srv://krishna:[email protected]/store? 
retryWrites=true&w=majority

you can find you username and password in from the mongo site.

CodePudding user response:

did you added database name that you wanted to be before question mark in the mongodb url

mongodb srv://<DATABASE_NAME>:<DATABASE_PASSWORD>@cluster0.mzive.mongodb.net/{right here}? 
retryWrites=true&w=majority
  • Related