Next.js Discord

Discord Forum

Couldnt connect to MongoDB !!!

Answered
Asian swamp eel posted this in #help-forum
Open in Discord
Avatar
Asian swamp eelOP
couldnt connect to mongoDB
making CRUD App using mongoDB in Next.js
Image
Image
Answered by Double-striped Thick-knee
make sure that you have the correct connection string and try to connect to it from MongoDB Compass
View full answer

21 Replies

Avatar
Double-striped Thick-knee
have you passed connection string to mongoose.connect() ?
Avatar
Asian swamp eelOP
import mongoose from "mongoose";

async function connectMongoDB() {
  try {
    await mongoose.connect(process.env.MONGODB_URI);
    console.log("Connected to MongoDB");
  } catch (error) {
    console.log(error);
  }
}

export default connectMongoDB;
 


yes !!!
Avatar
Double-striped Thick-knee
is it in your .env MONGODB_URI
Avatar
Asian swamp eelOP
yes

MONOGODB_URI=mongodb+srv://<ID>:<PASSWORD>@cluster0.asnnm.mongodb.net/
crud_db

 
Avatar
Double-striped Thick-knee
bruh, there is a typo. it's MONGODB_URI not MONOGODB_URI 😆
Avatar
Asian swamp eelOP
thx mate trying 😄
Image
still got error !!!
Avatar
Double-striped Thick-knee
this error is related to how your mongodb atlas is configured. you have to whitelist your ip to access mongodb.
Avatar
Asian swamp eelOP
since i am using configuration of ip `0.0.0.0/0
Avatar
Double-striped Thick-knee
hmmm ok, did you write the database name at the end of connection string
<PASSWORD>@cluster0.asnnm.mongodb.net/myDatabase
Avatar
Asian swamp eelOP
https://www.youtube.com/watch?v=wNWyMsrpbz0&t=3s
i am watching video from here !!!
Image
Image
Avatar
Double-striped Thick-knee
make sure that you have the correct connection string and try to connect to it from MongoDB Compass
Answer
Avatar
Asian swamp eelOP
ok trying !!!
Avatar
Asian swamp eelOP
its a small issue that causing the error was VPN since twitter ban hi my country so i use twitter when i disconnect from VPN and try to POST data then work !!!
Avatar
Double-striped Thick-knee
Alright, Mark Solution
Avatar
Asian swamp eelOP
Done !!!