問(wèn)題描述
我最近遇到了一個(gè)問(wèn)題.
I've recently run into a problem.
在過(guò)去的幾周里,我一直在研究我制作的一些不和諧機(jī)器人,但在導(dǎo)出客戶端對(duì)象時(shí)遇到了麻煩.我一直在嘗試將它導(dǎo)出到其他文件,這樣我就可以使用同一個(gè)對(duì)象將事件偵聽(tīng)器掛接到其他文件中.以下是我嘗試做的事情.
For the past several weeks I have been working on some discord bots I have made and am having trouble with exporting the client object. I've been trying to export it to other files so I can hook event listeners in other files using the same object. Below's what I attempted doing.
main.js
const client = new Discord.Client(); //Defining the client
exports.squidly = client; //Attempting to export the client
test.js
const client = require('../squidly').squidly;
client.on('ready', () => {
console.log("Test");
});
在閱讀了有關(guān)導(dǎo)出模塊的內(nèi)容后,我認(rèn)為這就是我所要做的,但每次我嘗試運(yùn)行它時(shí),它都會(huì)指出客戶端未定義.它總是給我消息無(wú)法讀取指向 test.js 中的偵聽(tīng)器的未定義的屬性'on'
After reading things about exporting modules I thought this was all I had to do but every time I try running this it states that client is undefined. It always gives me the message "cannot read property 'on' of undefined' pointing towards the listener in test.js
我在網(wǎng)上閱讀了許多不同的資源,但似乎沒(méi)有一個(gè)可以幫助我解決問(wèn)題.任何幫助,將不勝感激.謝謝
I've read many different sources online and none of them seemed to help me solve the issue. Any help would be appreciated. Thanks
推薦答案
如果您創(chuàng)建和導(dǎo)出客戶端的文件名為 main.js
,則您的 require
陳述是錯(cuò)誤的.改成
If the file in which you create and export the client is called main.js
, your require
statement is wrong. Change it to
const client = require('../main').squidly; //The require needs to point to the path and file name in which you export your client
這篇關(guān)于Discord 客戶端對(duì)象在導(dǎo)出后未定義的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!