How to import a variable from an event

时间:2019-03-19 14:57:52

标签: node.js discord.js

sugestion.on('collect', a => {
 const Suggestion = a.content;
});
console.log(`Suggestion: ${Suggestion}`);

I'm with TypeError with the above code.

(node:4872) UnhandledPromiseRejectionWarning: ReferenceError: Suggestion is not defined

2 个答案:

答案 0 :(得分:0)

You are trying to reference a variable out of scope. The variable Suggestion only exists within the scope of the function (a=>{}). Either move the console.log line into the function, or save the value in another variable outside the scope, for future reference.

答案 1 :(得分:0)

You need to have the Suggestion variable in scope

let Suggestion;
sugestion.on('collect', a => {
 Suggestion = a.content;
});
console.log(`Suggestion: ${Suggestion}`);