简体   繁体   English

解析 JSON 字符串化变量返回未定义的值?

[英]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 .下面的代码是我的 discord.js message事件的全部内容,我正在使用discord.jsnode-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() .它使用JSON.stringify()发回数据。 However when I try and parse it, everything I log only returns undefined.但是,当我尝试解析它时,我记录的所有内容都只返回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()JSON.parse()控制台日志的实际响应:

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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