Home > Mobile >  how to connect to mongodb from express?
how to connect to mongodb from express?

Time:02-10

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"));

CodePudding user response:

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

CodePudding user response:

Finally found a solution. Thanks for the help @ahmed ali.

In db uri, username and password should be written without angular brackets and replace the first database with your database name created.

  • Related