我需要从以下内容中提取指示值
{
"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.Induction
我收到错误
无法读取未定义的属性“字段”
这是我的代码:
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();
},
};
最佳答案
现阶段您的 JSON 还不是真正的 JSON。您需要parse您从 Ajax 请求中获得的结果,然后将其作为 native JSON 使用。
响应在发送给您时会被字符串化,该解析命令将为您将其字符串化。
您可以在您的 Promise 之后将其作为 then
链的一部分来执行(可能是最佳实践),或者直接在您的 Promise 内执行此操作。
关于javascript - 在 GET 请求中从 Node.js 中的 JSON 读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57154743/