I'm developing a ReactJS project that requires text to be read from an SVG file in the Public folder of the project directory, and then for global arrays to be populated with this information for other functions to utilize. I'm able to correctly read and parse out the data with a fetch method, but when I access the global variables after the function has ran, there is no change. However, when accessing the global variables from inside the method, the changes are there, but only within the scope of the fetch method, which is the main issue. How can I set a global variable to the results of a fetch method?
Here is the function: Note, that nodesArrayX and nodesArrayY are the global variables that I need to access and affect globally outside the scope of the fetch method.
function readNodeData() {
var coordsDisplay = document.getElementById("current-coords");
let testDataSet = [];
fetch('/ccmNodetest.svg')
.then(response => response.text())
.then(data => {
testDataSet = data.split("\n")
init()
//PARSE DATA
var nodeDataLines = data.split("\n")
var nodeDataStart = nodeDataLines.findIndex(element => element.includes("inkscape:label=\"NODES\""))
nodeDataLines.splice(0,nodeDataStart)
//coordsDisplay.innerText = nodeDataLines[0]
var nodeDataParseCount = 1
while (nodeDataLines[nodeDataParseCount] != " </g>") {
nodeDataParseCount += 3
var testingNodesData = nodeDataLines[nodeDataParseCount]
testingNodesData = testingNodesData.substring(11)
testingNodesData = testingNodesData.slice(0,-1)
nodesArrayX.push(testingNodesData)
nodeDataParseCount += 1
testingNodesData = nodeDataLines[nodeDataParseCount]
testingNodesData = testingNodesData.substring(11)
testingNodesData = testingNodesData.slice(0,-1)
nodesArrayY.push(testingNodesData)
nodeDataParseCount += 2
}
//PARSE DATA
});
function init() {
console.log(testDataSet);
}
}
Overall, the main issue is I can use a fetch method to access the file from the Public folder, read it, parse it out and push the new entries to the global arrays stated above. However, after the function has ran, the changes are not present, and are only present when calling the variables inside the scope of the fetch method. Any suggestions would be greatly appreciated, been stuck on this for a long time. Thank you!