Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - can't connect to mongdb nodejs

i'm facing some issue when trying to connect to mongodb using my nodejs app. heres the code

const { MongoClient } = require("mongodb");

async function main() {
  const uri =
    "mongodb+srv://xxxx:[email protected]/xxxx?retryWrites=true&w=majority";
  const client = new MongoClient(uri);
  await client.connect();
  await listDatabases(client);
  try {
    await client.connect();
    await listDatabases(client);
  } catch (e) {
    console.error(e);
  } finally {
    await client.close();
  }
}

main().catch(console.error);

and heres the Output:

Error: queryTxt ETIMEOUT xxxx.uj545.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19) {
  errno: undefined,
  code: 'ETIMEOUT',
  syscall: 'queryTxt',
  hostname: 'xxxx.uj545.mongodb.net'
}

But when I tried to do a test connection with Mongodb Compass, I got the same problem.

then I tried Mongodb Compass using Windows VPS from the Google Cloud Platform, and it worked,

then, where did it go wrong?

the device that I use is Macbook Air early 2015 with MacOs mojave


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

1 Answer

0 votes
by (71.8m points)

I would recommend you not to use raw connection instead you can use mongoose as an ODM to work with it here is the ref link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...