为啥我在nodejs中的代码会出错并要求我写catch,但我已经在代码中说了
Posted
技术标签:
【中文标题】为啥我在nodejs中的代码会出错并要求我写catch,但我已经在代码中说了【英文标题】:why does my code in nodejs gives error and asks me to write catch but i have already said it in the code为什么我在nodejs中的代码会出错并要求我写catch,但我已经在代码中说了 【发布时间】:2021-04-21 05:02:40 【问题描述】:所以这是错误
(node:24077) UnhandledPromiseRejectionWarning: TypeError: Cannot set property 'id' of undefined
at Client.msg (/app/bot.js:18:22)
at Client.emit (events.js:196:13)
at MessageCreateAction.handle (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/ws/7.4.2/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (events.js:196:13)
at Receiver.receiverOnMessage (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/ws/7.4.2/node_modules/ws/lib/websocket.js:825:20)
(node:24077) 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)
(node:24077) [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.
这是代码
console.log('loding')
const Discord = require('discord.js');
const client = new Discord.Client();
const fetch = require('node-fetch')
client.login(process.env.SECRET);
client.on('ready', () =>
console.log(`Logged in as $client.user.tag!`);
);
client.on('message', msg)
async function msg()
这是 Discord 频道 ID
if (msg.channel.id = '791583092275937303' )
if (msg.content === 'ping')
msg.reply('Pong!');
else if (msg.content === '!gif')
msg.reply('gif');
let url = `https://api.tenor.com/v1/search?q=codingtrain&key=$process.env.tenor&limit=8`
wow().then(response =>
console.log('ok')
msg.reply('ok')
).catch(err =>
console.log(err);
);
async function wow()
let response = await fetch(url)
let blob = await response.json();
这是错误前的控制台
node bot.js
loding
Logged in as cybemachine#8971!
这是 Discord 服务器链接:- https://discord.gg/CvCBUsyN
【问题讨论】:
【参考方案1】:您在消息事件之外使用msg
,因此msg
未定义。您还在第一个 if 条件中说 =
,但它必须是 ===
或 ==
,因为您想知道 id 是否等于您提供的 id。仅使用=
意味着您要设置变量的值。您必须将代码更新为:
console.log('loding')
const Discord = require('discord.js');
const client = new Discord.Client();
const fetch = require('node-fetch')
client.login(process.env.SECRET);
client.on('ready', () =>
console.log(`Logged in as $client.user.tag!`);
);
client.on('message', async msg =>
if (msg.channel.id === '791583092275937303')
if (msg.content === 'ping')
msg.reply('Pong!');
else if (msg.content === '!gif')
msg.reply('gif');
let url = `https://api.tenor.com/v1/search?q=codingtrain&key=$process.env.tenor&limit=8`
wow().then(response =>
console.log('ok')
msg.reply('ok')
).catch(err =>
console.log(err);
);
async function wow()
let response = await fetch(url)
let blob = await response.json();
)
【讨论】:
没错,但顺便说一句,if (msg.channel.id = '791583092275937303')
不应该是if (msg.channel.id === '791583092275937303')
?
@DmitriyFrolov 你说得对,我错过了,对不起。编辑了我的答案。以上是关于为啥我在nodejs中的代码会出错并要求我写catch,但我已经在代码中说了的主要内容,如果未能解决你的问题,请参考以下文章