0

The code below is the entirety of my discord.js message event, I am using discord.js as well as node-wit. When wit identifies a message including a math expression, it will evaluate the value and send it back to the user.

It sends back data using JSON.stringify(). However when I try and parse it, everything I log only returns undefined.

client.on('message', (message) => {
 wClient
  .message(message.content, {})
  .then((data) => {
   const response = JSON.stringify(data, ['intents', 'name', 'confidence']);
   const responseParsed = JSON.parse(response);

   console.log(response);
   console.log(responseParsed);

   if (responseParsed.name == 'Math') {
    message.channel.send(eval(data));
   }
  })
  .catch(console.error);
});

The actual response of the console logs for the JSON.stringify() and then the JSON.parse() are listed below:

JSON.Stringify()

{"intents":[{"name":"Math","confidence":0.9945}]}

JSON.parse()

{ intents: [ { name: 'Math', confidence: 0.9945 } ] }
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

0

based on this structure

 { intents: [ { name: 'Math', confidence: 0.9945 } ] }

I assume that this is how it should be to try

 if (responseParsed.intents[0].name == 'Math') {
message.channel.send(eval(data));

}

romanown
  • 325
  • 2
  • 9