Search code examples
javascriptnode.jstypescriptmongodbdiscord.js

Connecting code to MongoDB database gives error: Error: querySrv ENODATA _mongodb._tcp.cluster0.h5prp.mongodb.net


I'm coding a discord bot and I want to connect it to a mongoDB database. However when doing this, I get this error,

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'
}

Here is my code:

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'
  })
})

I looked through documentation and other posts but could not find anything that solves my problem. I'm using Node.js v17.1.0. I'm using WOKCommands and following this tutorial series https://youtube.com/playlist?list=PLaxxQQak6D_f4Z5DtQo0b1McgjLVHmE8Q

I've allowed all IP Addresses to access my database, I've ensured my IP address is whitelisted

Any help appreciated.

Thanks


Solution

  • I needed to change the mongoDB connection URI to an earlier Node.js version.

    I changed it to version '2.2.12 or later' and it worked. mongoDB connection

    Which game me a link like this: 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