react-excel-renderer
react-excel-renderer copied to clipboard
TypeError: Cannot read property 'map' of undefined
i am getting json data from excel file. but when i use outtabele i am getting this error
_**TypeError: Cannot read property 'map' of undefinedpai
**_
@avinashstunts123 were you able to solve this issue??
I just saw this error in my app. In my case the problem was here:
ExcelRenderer(file, (error: Error, response: any) => {
if (error) {
console.error(error);
} else {
setRows(response.rows);
setColumns(response.columns); // <-- here's what's wrong
}
})
The correct property is response.cols
, not response.columns
, so I was writing undefined
to into state instead of the actual columns data. Hence the inability for React to read the .map()
method from what should have been an array but was in fact undefined
.
Hello , sorry for my english i resolve this, it's work for me .
`
const [cols,setCols]=useState([]); const [rows,setRows]=useState([]);
const fileHandler = (event) => {
let fileObj = event.target.files[0];
//just pass the fileObj as parameter
ExcelRenderer(fileObj, (err, resp) => {
if(err){
console.log(err);
}
else{
setCols(resp.cols)
setRows(resp.rows)
}
});
}
return (
<div>
<input type="file" onChange={fileHandler.bind()} style={{"padding":"10px"}} />
<OutTable data={rows} columns={cols} tableClassName="ExcelTable2007" tableHeaderRowClass="heading" />
</div>
)
`
I configure useState with empty Array.