Javascript can';t连接到mongdb nodejs
我在尝试使用我的nodejs应用程序连接mongodb时遇到了一些问题。 这是密码Javascript can';t连接到mongdb nodejs,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我在尝试使用我的nodejs应用程序连接mongodb时遇到了一些问题。 这是密码 const { MongoClient } = require("mongodb"); async function main() { const uri = "mongodb+srv://xxxx:xxxx@xxxx.uj545.mongodb.net/xxxx?retryWrites=true&w=majority"; const client
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);
这是输出:
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'
}
但当我尝试与Mongodb Compass进行测试连接时,我遇到了同样的问题
然后我在谷歌云平台上使用Windows VPS尝试了Mongodb Compass,它成功了
那么,哪里出了问题
我使用的设备是Macbook Air 2015年初的MacOs mojave如果在mongodb云中创建db,则需要在mongodb云面板中的网络访问中配置ip地址
如果您想允许从任何ip访问,请使用0.0.0.0/0,或者输入您的ip。我建议您不要使用原始连接,您可以使用mongoose作为ODM来使用它
这里是ref可能问题出在mongodb设置中的网络访问配置中我已将配置设置为接受所有传入的ip地址[0.0.0.0/0],但我仍然面临相同的问题OK,尝试从uri中删除“+srv”。或者在集群的连接形式中选择“连接您的应用程序”,然后选择“nodejs”,并使用“2.2.12或更高版本”来生成uri。