Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

mongoose - Not able to connect to mongoDB atlas

I have installed everything properly but don't know what had happened. This is the code

// Connect to MongoDB
const dbURI = 'mongodb+srv://<username>:<password>@blogging-site.mantx.mongodb.net/Blogging-site?retryWrites=true&w=majority';
try {
  // Connect to the MongoDB cluster
   mongoose.connect(dbURI,{ useNewUrlParser: true, useUnifiedTopology: true },
    () => console.log(" Mongoose is connected")
  );
} catch (e) {
  console.log("could not connect");
}

The error in my VS code treminal is - Error: queryTxt ETIMEOUT blogging-site.mantx.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19) { errno: undefined, code: 'ETIMEOUT', syscall: 'queryTxt', hostname: 'blogging-site.mantx.mongodb.net' } I am following net Ninja tutorials and hope i haven't left any step as he instructed. I had earlier used mongoDB atlas and have mongodb installed in my computer. Does that create any problem ?

question from:https://stackoverflow.com/questions/65889737/not-able-to-connect-to-mongodb-atlas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There's no problem as 'I could connect' to it.

I could connect to it

Me, someone you don't know, someone who is most probably in the opposite side of the world

Someone who could potentially be a person with bad intentions(although I am not).

It was probably a connection problem(due to bad internet or something) but the bigger problem is

I could connect to it

So maybe you would want to leave the MongoDB URI part out before you make it public for everyone to see, unless you want an evil person to steal user-infos, session datas, and what not from your database.

Happy learning!!!

Edit after the comment Here's what you should learn to get started in mongoose:

  -setting up initial(also called default) connection with mongoose.connect()
  -configuring schemas with mongoose.Schema()
  -compiling models with schemas with mongoose.model()
  -saving the doc with mongoose.save()
  -querying the database(findOne, find, deleteOne, updateOne, findById are the 
   ones I find most used)

And maybe when you get used to all those, you can move to learning populate(you're probably gonna need it in larger projects), creating multiple connections and export schema pattern, validation, middlewares of mongooseJs and many more that I'm sure you'll look into by yourself.

As for the "can you tell me how had you got connected to the atlas?":

I just copied your code and ran in on my computer. Simple as that. That string in dbURI variable is a way to connect to your database atlas. You gave it to me, I used it and connected to it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...