node.js - 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); 时出现上述错误;如果您需要更多信息,请告诉我。
解决方案
也许尝试类似的东西
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)
推荐阅读
- regex - 使用 Awk 使用正则表达式和循环对字段求和
- reactjs - 使用 React 路由器和 .Net Core 服务器应用程序调用 Auth0 回调时出现 404
- c - kmalloc() 调用后的内存块中有什么?kcalloc()?
- html - 视口在 localhost 8080 中自动更改
- python - 如何在python中将列表更新为全0
- javascript - 错误错误:未捕获(承诺):错误:NodeInjector:NOT_FOUND [ControlContainer]
- javascript - 如何在firebase react js中向经过身份验证的用户添加集合?
- elasticsearch - 如果计划任务在下一个计划时间开始之前没有完成,会发生什么?
- r - JuliaCall 中的致命错误:找不到符号“ccalllib_libR.dylib74”
- r - R:来自元素向量的所有长度的所有组合,每个元素具有2个条件