【问题标题】:MongoServerSelectionError: getaddrinfo ENOTFOUND at Timeout._onTimeoutMongoServerSelectionError:getaddrinfo ENOTFOUND 在 Timeout._onTimeout
【发布时间】:2021-12-09 21:35:08
【问题描述】:

所以您可能在其他地方看到过这种问题类型。但这里的事情是我根据文档尝试了我们需要做的所有事情,并看到其他有类似错误的帖子,但我的错误仍然没有修复。所以我在这段代码中使用 keyv 并使用 MongoDB atlas 作为存储适配器,但错误来自 MongoDB。此外,“keyv”中没有错误,因为它适用于其他人,MongoDB 中有错误

所以现在我将列出我尝试过的所有内容:

1. Made sure there is IP access
2. The userid and passcode are correct 
3. The MongoDB atlas is running 
4. Read the docs and code multiple times
5. If u think adding the +srv with the code will fix the error, it won't, it doesn't work with keyql idk why also it is not present in many codes, I already tried it 

这就是代码

const { Client, Intents, MessageEmbed, Collection } = require('discord.js');
let client = new Client({ intents: [Intents.FLAGS.GUILDS,Intents.FLAGS.GUILD_MESSAGES] });
const dotenv = require('dotenv');
const Keyv = require('keyv');
const keyv = new Keyv('mongodb://Discord:password@cluster0.auifa.mongodb.net/Cluster0');
dotenv.config();

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});


client.on('messageCreate', async (msg) => {
     if (msg.author.bot) return;
    let number = msg.content.split(' ')[1];
    if (msg.content === '!ping') {
        msg.channel.send('ping!')
    }

    
// Use like const prefix = await getGuildPrefix(); `
    const getGuildPrefix = async () => {
        const prefixMap = await keyv.get('prefix');
        return prefixMap ?. [msg.guild.id] || "!"
    }

// Sets the prefix to the current guild.
    const setGuildPrefix = async (prefix) => {
        let prefixMap = await keyv.get('prefix');
        if (!prefixMap)
    {
     prefixMap = "!";
    }
        prefixMap[msg.guild.id] = prefix;
        await keyv.set('prefix', `${prefixMap}`);
    }

    let prefix = await getGuildPrefix();
// Get prefix command.
    if ((msg.content === `${process.env.prefix}prefix`) || (msg.content === `${prefix}prefix`)) {
        msg.channel.send(`Your server prefix is ${prefix}`)
    }

// Change prefix command
    const commandPrefix = await getGuildPrefix();
    if ((msg.content.startsWith(`${process.env.prefix}setprefix`)) || (msg.content.startsWith(`${commandPrefix}setprefix`))) {
        const newPrefix = number;

        if (newPrefix.length === 0) {
            msg.channel.send(`Please enter a valid prefix`);
        }

        await setGuildPrefix(newPrefix)
        msg.channel.send(`Your server prefix is now '${newPrefix}'`);
    }
})
client.login(process.env.token);

这是错误信息

Keyv connection error: MongoServerSelectionError: getaddrinfo ENOTFOUND cluster0.auifa.mongodb.net
    at Timeout._onTimeout (D:\javascript\node_modules\mongojs\node_modules\mongodb\lib\core\sdam\topology.js:438:30)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7) {
  reason: TopologyDescription 

【问题讨论】:

    标签: javascript node.js mongodb discord.js


    【解决方案1】:

    连接字符串看起来不像 Atlas 的。
    它必须类似于:mongodb+srv://<username>:<password>@cluster0.auifa.mongodb.net/YOUR-DB

    然后登录您的 Atlas 帐户:

    1. 转到数据库页面
    2. 点击连接按钮
    3. 选择“连接您的应用程序”
    4. 复制您的连接字符串

    关于 Mongo Atlas 连接的文档:https://docs.atlas.mongodb.com/connect-to-cluster/#connect-to-a-cluster

    【讨论】:

    • 阅读我尝试过的第 4 点和第 5 点,我试过了,但它不起作用,不知道为什么
    • 因为 Keyv 使用 mongojs 而不是官方的 MongoDB 驱动:github.com/jaredwray/keyv-mongo/issues/…
    • 是的,它与我在代码中给出的格式相同(代码中的第 5 行),我说我都试过了
    • 使用这个版本:"dependencies": { "@keyv/mongo": "github:darovic/keyv-mongo",
    • 已经这样做了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 2022-06-23
    • 2018-04-21
    相关资源
    最近更新 更多