【问题标题】:Reading values from JSON in Node.js in GET request在 GET 请求中从 Node.js 中的 JSON 读取值
【发布时间】:2019-07-22 23:01:47
【问题描述】:

我需要从下面提取指示值

{
    "records": [{
        "id": "recBgV3VDiJeMkcwo",
        "fields": {
            "DrugName": "azatadine",
            "nameapi": ["recBgV3VDiJeMkcwo"],
            "Indication": "For the relief of the symptoms of upper respiratory mucosal congestion in perennial and allergic rhinitis, and for the relief of nasal congestion and eustachian t.b. congestion.",
            "lookup": ["azatadine"],
            "drugID": "recBgV3VDiJeMkcwo"
        },
        "createdTime": "2018-11-09T19:38:24.000Z"
    }]
 }

当我尝试做 response.records[0].fields.Indication 我得到错误 Cannot read property 'fields' of undefined

这是我的代码:

function httpGet() {

return new Promise(((resolve, reject) => {

var options = {

host: 'api.airtable.com',

port: 443,

path: '/v0/appYqfJ3Rt2F0sRGn/Database?filterByFormula=(DrugName=%27azatadine%27)',

method: 'GET',

headers: {

Authorization: 'Bearer key123456789'

}

};

const request = https.request(options, (response) => {

response.setEncoding('utf8');

let returnData = '';

response.on('data', (chunk) => {

returnData += chunk;

});

response.on('end', () => {

resolve(returnData);

});

response.on('error', (error) => {

reject(error);

});

});

request.end();

}));

}



const UserReplyIntent_Handler = {

canHandle(handlerInput) {

const request = handlerInput.requestEnvelope.request;

return request.type === 'IntentRequest' && request.intent.name === 'UserReplyIntent' ;

},

async handle(handlerInput) {

const response = await httpGet();

console.log(response);

return handlerInput.responseBuilder

.speak("Okay. Here we go" + response.records[0].fields.Indication)

.reprompt("say again")

.getResponse();

},

};

【问题讨论】:

    标签: javascript arrays node.js get alexa-skills-kit


    【解决方案1】:

    在这个阶段,您的 JSON 还不是真正的 JSON。您需要 parse 从 Ajax 请求中获得的结果,然后将其作为本机 JSON 使用。

    响应在传递给您时会被字符串化,该解析命令将为您将其字符串化。

    您可以在您的承诺之后将其作为then 链的一部分执行(可能是最佳实践),或者直接在您的承诺中执行。

    【讨论】:

    • 你介意分享一个如何做到这一点的例子吗?对不起,我在这方面完全是菜鸟
    • 实际上,最好的方法可能是response.json() - MDN 上有一个很好的例子,它可能比我在这里能解释得更好,我只是指那篇文章无论如何,所以我不会抄袭它们:) developer.mozilla.org/en-US/docs/Web/API/Body/json
    【解决方案2】:

    我错过了 json.parse

    .then((response) => {
          const data = JSON.parse(response);
     for (let i = 0; i < data.records.length; i++) {
     if (i === 0) {
         outputSpeech = outputSpeech +  'The name of the drug is' + data.records[i].fields.DrugName +  ', '
    

    【讨论】:

      【解决方案3】:

      我错过了 json.parse 方法

      .then((response) => {
            const data = JSON.parse(response);
       for (let i = 0; i < data.records.length; i++) {
       if (i === 0) {
           outputSpeech = outputSpeech +  'The name of the drug is' + data.records[i].fields.DrugName +  ', '
      

      【讨论】:

        【解决方案4】:

        使用异步响应执行此操作的标准方法是执行以下操作:

        .then((response) => response.json())
        .then((response) => {
          //response is now a JSON object that you can be worked with
        })
        

        【讨论】:

          猜你喜欢
          • 2014-09-13
          • 1970-01-01
          • 1970-01-01
          • 2017-05-03
          • 1970-01-01
          • 2014-09-30
          • 2014-11-11
          • 1970-01-01
          • 2018-06-20
          相关资源
          最近更新 更多