首页 > 解决方案 > DiscordAPIError:无法发送空消息。但是我可以记录我试图发送到控制台的东西,发送的数据来自 MongoDB

问题描述

Code:
const ModSchema = require('../models/ModSchema');

module.exports = {
    name: 'warns',
    description: "lists a members warns.",
    execute(message, args, client, Discord){
        const UserIDsearch = message.mentions.members.first().id;
        ModSchema.findOne({ 'UserID': UserIDsearch }, function (err, Punishments) {
            if (err) return console.log(err);
            let data = Punishments;
            console.log(data);
            message.channel.send(data);
        })
    }
}
Error Message:
(node:7397) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message
    at RequestHandler.execute (/Users/mynamegoeshere/ESENTRIX CLONE FILE/Esentrix-Bot-5/node_modules/discord.js/src/rest/RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async RequestHandler.push (/Users/mynamegoeshere/ESENTRIX CLONE FILE/Esentrix-Bot-5/node_modules/discord.js/src/rest/RequestHandler.js:39:14)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:7397) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7397) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

What I want to send in Discord: 
"Punishments": [
    {
      "PunishType": "Mute",
      "Moderator": "Poetic",
      "Reason": "test",
      "Time": "10000 ms"
    }

这是用于从 MongoDB 数据库查询数据的代码,数据存储为 JSON 文件,但我认为如果将其设置为 const/let,它会被字符串化。请让我知道如何发送数据,因为我似乎找不到问题。请记住,我尝试发送的文本可以使用 console.log(data); 记录到控制台,但是在使用 message.channel.send(data); 时出现上述错误;如果您需要更多信息,请告诉我。

标签: node.jsmongodbdiscorddiscord.js

解决方案


也许尝试类似的东西

module.exports = {
    name: 'warns',
    description: "lists a members warns.",
    execute(message, args, client, Discord){
        const UserIDsearch = message.mentions.members.first().id;
        ModSchema.findOne({ 'UserID': UserIDsearch }, function (err, Punishments) {
            if (err) return console.log(err);
            if(Punishments){
                let data = Punishments;
                console.log(data);
                message.channel.send(data);
            }
        })
    }
}

或者只是为了确保它的字符串化,尝试

JSON.stringify(data)

推荐阅读