简体   繁体   English

无法连接到 mongdb nodejs

[英]can't connect to mongdb nodejs

i'm facing some issue when trying to connect to mongodb using my nodejs app.尝试使用我的 nodejs 应用程序连接到 mongodb 时遇到一些问题。 heres the code这是代码

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

async function main() {
  const uri =
    "mongodb+srv://xxxx:xxxx@xxxx.uj545.mongodb.net/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:这是 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.但是当我尝试用 Mongodb Compass 进行测试连接时,我遇到了同样的问题。

then I tried Mongodb Compass using Windows VPS from the Google Cloud Platform, and it worked,然后我使用来自谷歌云平台的 Windows VPS 尝试了 Mongodb Compass,它成功了,

then, where did it go wrong?那么,go 哪里错了?

the device that I use is Macbook Air early 2015 with MacOs mojave我使用的设备是 2015 年初的 Macbook Air 和 MacOs mojave

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我建议您不要使用原始连接,而是可以使用 mongoose 作为 ODM 来使用它,这是参考链接

If you create the db in mongodb cloud, you need to configure the ip address in Network Access in the mongodb cloud panel.如果在mongodb云中创建db,则需要在mongodb云面板的网络访问中配置ip地址。

use 0.0.0.0/0 if you want to allow access from any ip, or enter your ip.如果您想允许从任何 ip 访问,请使用 0.0.0.0/0,或输入您的 ip。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM