Currently, I am utilizing Papa Parse along with Angular 2 to import a CSV list and then wish to pass that list to another component. While I can successfully read the CSV data and display them using console log, I am facing challenges in accessing the parsed data outside of the 'complete' function due to the asynchronous nature of parsing files. Although I attempted to address this by implementing a callback function, I still encounter difficulties in utilizing the data from Papa Parse's 'complete' function elsewhere.
Here is an example of my attempt where the listOfLoads variable remains undefined in the convertloads method but functions normally in synchronous methods:
export class AppComponent {
listOfLoads: Load[] = [];
constructor(){
}
importFile(fileInput: any, callBack) {
let listOfLoads: Load[] = [];
let file = fileInput.target.files[0];
let result = Papa.parse(fileInput.target.files[0], {
complete: function (results) {
callBack(results.data);
},
header: true,
delimiter: ";"
});
}
convertLoads(data) {
this.listOfLoads = deserialize<Load[]>(Load, data);
}
}
Is there a way to store the data in a list and make use of it in another component? I have been looking for a solution far and wide.