网站搜索

javascript - 将代码连接到 MongoDB 数据库时出错:错误:querySrv ENODATA _mongodb._tcp.cluster0.h5prp.mongodb.net


我正在编写一个 Discord 机器人,并且我想将它连接到一个 MongoDB 数据库。然而,在执行此操作时,我遇到了这个错误,

Error: querySrv ENODATA _mongodb._tcp.cluster0.h5prp.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (node:dns:228:19) {
  errno: undefined,
  code: 'ENODATA',
  syscall: 'querySrv',
  hostname: '_mongodb._tcp.cluster0.h5prp.mongodb.net'
}

这是我的代码:

client.on('ready', () => {
    console.log('The bot is ready!')
  const dbOptions = {
    // These are the default values
    keepAlive: true
  }
  new WOKCommands(client, {
    // The name of the local folder for your command files
    commandsDir: path.join(__dirname, 'commands'),
    // Allow importing of .ts files if you are using ts-node
    typeScript: true,
    // Pass in the new dbOptions
    dbOptions,
    // Pass in your own mongo connection URI
    mongoUri: 'mongodb+srv://discordbot:******@cluster0.h5prp.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'
  })
})

我查阅了文档和其他帖子,但没有找到能解决我问题的内容。我正在使用 Node.js v17.1.0。我正在使用 WOKCommands,并且正在跟随这个教程系列 https://youtube.com/playlist?list=PLaxxQQak6D_f4Z5DtQo0b1McgjLVHmE8Q

我已经允许所有IP地址访问我的数据库,我已经确保我的IP地址在白名单中

任何帮助都将不胜感激。

谢谢

答案#1

我需要将 mongoDB 连接 URI 更改为较早的 Node.js 版本。

我把它改成了版本 '2.2.12 或更高',然后它就能工作了。

哪个游戏给我一个像这样的链接:mongodb://discordbot:<password>@cluster0-shard-00-00.h5prp.mongodb.net:27017,cluster0-shard-00-01.h5prp.mongodb.net:27017,cluster0-shard-00-02.h5prp.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-g86xz1-shard-0&authSource=admin&retryWrites=true&w=majority