Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to connect to mongodb from express?

Tried to connect To mongodb, it says connection not found. can someone point out what error I have made in code, is there any problem in db connect URI

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

dotenv.config();

const db =
  "mongodb+srv://<sumit>:<sumit>@bike-ecommerce.u7sod.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";

console.log(db);
mongoose
  .connect(db)
  .then(() => {
    console.log("connection successful");
  })
  .catch((err) => console.log(err));

app.use(express.json());

//importing routes

const authRoute = require("./routes/auth");

//route middle wares
app.use("/api/user", authRoute);

app.listen(3000, () => console.log("gg server is running"));

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is a snippet from my own application it works there

mongoose.connect(dbUrl).then((dbo)=>{
  console.log("DB connected")
},(err)=>{
  console.log("error")
});

I have connected like this your then is missing a variable tell me if this dosnt work

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading