I have a dropdown that I want to use to set a category and then save to my files page. Files is in an array of the Well interface. When I got to change the value, i get back this error:
TypeError: Cannot set properties of undefined (setting 'category')
src/Components/Main/WellWorkflow/WellFiles/WellFiles.tsx:181
178 |
179 | private handleInputChange = (index: number) => (event: any) => {
180 | const well = this.state.well;
> 181 | well.files[index].category = event.target.value;
182 | console.log(well);
183 | this.setState({
184 | well
Here is my code
private handleInputChange = (index: number) => (event: any) => {
const well = this.state.well;
well.files[index].category = event.target.value;
console.log(well);
this.setState({
well
});
}
public renderDropdown = () => (category: string, index: number) => {
return (
<FormGroup >
<EInput
type="select"
name="Category"
value={this.fileCategory.category}
onChange={this.handleInputChange(index)}
>
<option value="" />
<option value="Not Categorized">Not Categorized</option>
<option value="Signed Documents">Signed Documents</option>
<option value="Unsigned Documents">Unsigned Documents</option>
<option value="3rd Party Documents">3rd Party Documents</option>
<option value="General Well Info">General Well Info</option>
</EInput>
</FormGroup>
);
}
private formatWellFiles = () => {
const files = this.state.well.files;
const headers = [
"File Name",
"Category",
"Size",
"Uploaded By",
"Upload Date",
"Download",
];
const rows = files.map(f => {
return [
{
content: f.name
},
{
content: this.renderDropdown(),
sortItem: f.category,
type: 'render'
},
{
content: this.renderSize(f.size),
sortItem: Number(f.size),
type: 'render'
},
{
content: f.createUser
},
{
content: f.createDate
},
{
content: this.renderDownload(f),
type: 'render',
},
];
});
I have done this other areas of the code but those go directly to the interface whereas well.files represents files: IAttachedFile [] as seen here:



well.files[index]expected to be? It sounds like the index of that array you are requesting hasn't been set.well.files[index]is undefined when that code is hit. This is fact based on the error. Eitherwell.filesdoes not have any or all of the data yet at that time, orindexis coming out of bounds forwell.files.