0

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 Interface

index

IAttached file when you click on definition

4
  • 1
    What is well.files[index] expected to be? It sounds like the index of that array you are requesting hasn't been set. Commented Nov 11, 2021 at 14:40
  • I thought about that too but I am not sure how to set it.. well.files[index] represents IAttachedFile and then .category is showing up like it should and when you go to the definition, it will take you straight to IAttachedFile.category.. ill post some pics above to show you more. Commented Nov 11, 2021 at 14:53
  • well.files[index] is undefined when that code is hit. This is fact based on the error. Either well.files does not have any or all of the data yet at that time, or index is coming out of bounds for well.files. Commented Nov 11, 2021 at 15:05
  • That makes sense, thank you for perspective on the problem. Commented Nov 11, 2021 at 15:16

2 Answers 2

1

Try making some form of check to see if there is an object at specific index eg:

private handleInputChange = (index: number) => (event: any) => {
  const well = this.state.well;
  if(well.files[index]){
    well.files[index].category = event.target.value;
    console.log(well);
    this.setState({
     well
    });
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

As was mentioned in the comments, well.files[index].category is probably undefined

If your version of TS is 4.1 or higher you can enable noUncheckedIndexedAccess option in tsconfig.json, then result of value accessed by index will be T | undefined for non-const arrays

Documentation on --noUncheckedIndexedAccess

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.