我正在尝试检索fromDate,toDate,beenViewed等元素,但这给了我类似的错误
无法读取未定义的属性“ 0”
我在做什么错?如何获取这些值?我想要这些元素的数组或变量,以便以后在程序中使用它们。这是我的代码:
const axios = require('axios');
const {parseString} = require('xml2js');
const {apiKey, baseURL} = require('../config/apiKeys.js');
const xmlBody = `xml body with ${apiKey}`;
axios.post(
baseURL,
xmlBody,
{
headers: {
'Content-Type': 'text/xml'
}
}
)
.then(response => {
parseString(response.data, (err, parsed) => {
parsed['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['ns1:wsdlGetLeaveApplicationsV2Response'][0]['return'][0]['leaveApplications'][0]['item'];
var qid = parsed.id[0]._;
console.log(qid);
//console.log(JSON.stringify(parsed, undefined, 1));
})
}).catch(err => {
console.log(`Error:${err}`);
});
JSON.stringify(parsed, undefined, 1); //gives the following response:
[
{
"$": {
"xsi:type": "tns:LeaveApplicationV2"
},
"id": [
{
"_": "122",
"$": {
"xsi:type": "xsd:int"
}
}
],
"header": [
{
"$": {
"xsi:type": "xsd:string"
}
}
],
"bodytext": [
{
"_": "sdfsfsaf",
"$": {
"xsi:type": "xsd:string"
}
}
],
,
"beenViewed": [
{
"_": "2",
"$": {
"xsi:type": "xsd:int"
}
}
],
"fromDate": [
{
"_": "2019-01-21",
"$": {
"xsi:type": "xsd:date"
}
}
],
"toDate": [
{
"_": "2019-01-22",
"$": {
"xsi:type": "xsd:date"
}
}
],