简体   繁体   中英

Parsing a JSON Stringified variable returns the value undefined?

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 } ] }

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));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM