I am getting data from excel and trying to save that data into an array. I want to push the objects into an empty array when I upload the excel file, but I am getting this error Uncaught TypeError: Cannot read properties of undefined (reading 'push')
. Here is my code:
<template>
<div id="app">
<input
type="file"
name="xlfile"
id="xlf"
v-on:change="displayFile($event)"
/>
<b-table striped hover bordered :items="itemsList"></b-table>
</div>
</template>
<script>
import * as XLSX from "xlsx/xlsx.mjs";
export default {
name: "App",
data() {
return {
itemsList: [],
};
},
methods: {
displayFile(e) {
var files = e.target.files,
f = files[0];
var reader = new FileReader();
reader.onload = function (e) {
var data = e.target.result;
var workbook = XLSX.read(data, { type: "binary" });
let sheetName = workbook.SheetNames[0];
let worksheet = workbook.Sheets[sheetName];
let rowObject = XLSX.utils.sheet_to_row_object_array(worksheet);
const finalJsonData = JSON.parse(
JSON.stringify(rowObject, undefined, 4)
);
finalJsonData.map((item) => {
this.itemsList.push(...item);
});
//console.log(typeof finalJsonData);
};
reader.readAsArrayBuffer(f);
},
},
};
</script>