连接 MongoDB 数据库时出错

连接 MongoDB 数据库时出错

我遇到了这个以前从未遇到过的错误。我没有更改任何代码,但突然遇到了这个奇怪的错误,它除了输出这个错误之外什么都不做(它不会执行我希望它使用我让它检索的数据库信息执行的代码)。

错误:

(node:11428) UnhandledPromiseRejectionWarning: MongoNetworkError: connection 5 to formbot-shard-00-02-rq3ga.azure.mongodb.net:27017 closed
    at TLSSocket.<anonymous> (C:\Users\seanh\Desktop\Important\AstroServices\FormBot\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:276:9)
    at Object.onceWrapper (events.js:272:13)
    at TLSSocket.emit (events.js:185:15)
    at _handle.close (net.js:538:12)
    at TCP.done [as _onclose] (_tls_wrap.js:379:7)
(node:11428) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

代码:

exports.run = (bot, message, args) => {
    if(!message.member.roles.has(`${message.guild.roles.find(role => role.name === bot.config.permRole).id}`))
        return message.channel.send(`Sorry, you do not have permission to run that command!`);
    const Discord = require('discord.js');
    const mongoose = require('mongoose');
    const fs = require('fs');

    mongoose.connect(`${bot.config.dbconnection}`, {
        useNewUrlParser: true
    });
    const messages = require('../models/messages.js');
    messages.find({
        serverID: message.guild.id
    }).sort([
        ['message', 'descending']
    ]).exec((err, res) => {
        if(err) console.log(err);

        let embed = new Discord.RichEmbed()
        .setTitle(`Message List`)
        .setColor(bot.config.embedColour)
        .setTimestamp()
        .setThumbnail(message.author.avatarURL)
        .setFooter(`Message list as of`)

        if(res.length === 0) {
            embed.addField(`Messages`, `No messages found`)
        } else if (res.length < 10) {
            for (i = 0; i < res.length; i++) {
                embed.addField(`${i + 1}. ${res[i].name}`, `**Message**: ${res[i].message}\n**Day**: ${res[i].day}`);
            }
        } else {
            for (i = 0; i < bot.config.dbMaxReadLength; i++) {
                embed.addField(`${i + 1}. ${res[i].name}`, `**Message**: ${res[i].message}\n**Day**: ${res[i].day}`);
            }
        }

        message.channel.send(embed);
    });
};

任何帮助都将不胜感激,因为没有人在 Slack 或 IRC 上回复,而我真的需要解决这个问题。

谢谢你,肖恩。

答案1

事实证明,我需要在我的 MongoDB IP 白名单中允许我连接的 IP。

相关内容