I am trying to use the JS fetch() coupled with the npm package xml2js to pull xml data from an API with react and display it with my frontend. I am able to do this with regular data but when it comes down to applying xml2js it is throwing back errors in relation to 'data' not being defined and such. The following code is what I have achieved so far:
fetch(URL)
.then(res => res.text())
.then(data => {
this.setState({
trains: ArrayOfObjStationData.objStationData
});
});
// convert xml to object
var parseString = require('xml2js').parseString;
parseString(data, (err, res) => {
console.log(res.ArrayOfObjStationData.objStationData);
this.setState({data: res.ArrayOfObjStationData.objStationData})
});
I think I am just incorrect about my formatting of the code but any help would be greatly appreciated.