javascript - 无法连接到 mongdb nodejs
问题描述
尝试使用我的 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 = 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 进行测试连接时,我遇到了同样的问题。
然后我使用 Google Cloud Platform 中的 Windows VPS 尝试了 Mongodb Compass,它成功了,
那么,到底哪里出错了?
我使用的设备是 2015 年初的 Macbook Air 和 MacOs mojave
解决方案
我建议您不要使用原始连接,而是可以使用 mongoose 作为 ODM 来使用它,这是参考链接
推荐阅读
- javascript - Vue.js v-bind:值不显示值
- sql - 如何在 CloudFoundry 上部署 Column View 工件
- c# - 使用 ID 列表过滤的 Elasticsearch 多重匹配
- java - GeoMap 未正确居中
- wordpress - 我想创建一个 Wordpress 存档页面,该页面显示带有匹配查询变量的标签的帖子。是否有任何插件可以帮助我做到这一点?
- ios - 我如何通过一些快速登录来使应用程序崩溃?
- python - 蟒蛇健身房。看不到环境
- apache-spark - 使用文件名创建变量 - PySpark
- pipeline - 如何在 Spinnaker 中将 base64 工件转换为 json/yaml?
- android - 通过自定义 pin 对用户进行身份验证以访问密钥库