I am struggling to find a simple answer, even though my code is simple. I attempted the following approach, but every time I try to console.log my testResult, it always returns null. How can I correctly save data from a file?
public getFile(
sourceFile: File
): string {
let testResult;
const file = sourceFile[0]
const fileReader = new FileReader();
fileReader.readAsText(file, "UTF-8")
fileReader.onloadend = (e) => {
testResult = fileReader.result.toString()
}
console.log(testResult)
return testResult
}
This issue ties back to previous topics where I struggled to handle loading a JSON file, translating it, and displaying it to the user. If I could save this file outside of onloadend, I believe I could overcome the remaining challenges (previous methods failed, and this one sets me back at the start).