-1
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

zero298
  • 25,467
  • 10
  • 75
  • 100
Vitu
  • 1
  • 1
  • 1
    Since you are getting a Promise rejection warning and are trying to leak the scope of an asynchronous variable, I think this is an AB problem and that you are going to need to read this eventually: [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086/691711). Don't accidentally make the scope of `Suggestion` too broad. – zero298 Mar 19 '19 at 15:05
  • Are you trying to print `a.content`? If you are, then declaring `Suggestion` outside the anonymous function scope will not work, since it will just print `undefined` (assuming `suggest.on` is async). – asleepysamurai Mar 19 '19 at 15:09

2 Answers2

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.

Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
  • Yes I know. I was wondering if there is how to import the variable from there, out. _Sorry for not posting, I'm new here._ – Vitu Mar 19 '19 at 15:04
  • Many ways to do this. One is to the define the variable `Suggestion` above the function. But don't use `const` if you're planning to assign a value to it later. Use `let`. – Traveling Tech Guy Mar 19 '19 at 15:06
  • Also if you are looking to log the value of `a.content`, and `suggest.on` is an async function, then you absolutely have to move the `console.log` to inside the function. Otherwise it'll just log `undefined`. – asleepysamurai Mar 19 '19 at 15:11
0

You need to have the Suggestion variable in scope

let Suggestion;
sugestion.on('collect', a => {
 Suggestion = a.content;
});
console.log(`Suggestion: ${Suggestion}`);
djheru
  • 3,525
  • 2
  • 20
  • 20