TypeError: Cannot read property 'number' of undefined

Viewed 1291

I would like to use Axios to delete a book from my list. I use the URL of my database + the id, but I can't find the way to specify the id in the URL and it stays undefined.

It looks like this :

export default class ListBooks extends React.Component {
    constructor(props) {
        super(props);
        this.state = { error: null, data: [] }
    }
    componentDidMount() {
        Axios.get(process.env.REACT_APP_API_PATH_BOOKS)
            .then(res => {
                this.setState({ data: res.data });
            })
            .catch(errorThrown => {
                this.setState({ error: errorThrown });
            })
    }

    handleChange = event => {
        this.setState({ number: event.target.value });
    }

        /**
        * Use to delete a book by the id.
        */
        handleDelete = () => {
            const id = this.data.number
            console.log(id);
            Axios.delete(process.env.REACT_APP_API_PATH_BOOKS + id)
                .then(res => {
                     console.log(res);
                    console.log(res.data);
                    let cible = document.getElementById("book-admin" + id);
                    cible.remove();
                })
                .catch(errorThrown => {
                    this.setState({ error: errorThrown });
                })
        }

        render() {
            const { data } = this.state;

            return (
                <div>
                    <Container>
                        {data.map(books =>
                            <div key={books.number}>
                                <ListGroup>
                                    <ListGroup.Item disabled id={"book-admin" + data.number}>{books.name} {books.author}
                                    </ListGroup.Item>
                                </ListGroup>
                                <Button variant="outline-warning" size="sm" className="btn-admin-change" id={data.number} onClick={this.props.handleUpdate}>Modifier</Button>
                                <Button variant="outline-danger" size="sm" className="btn-admin-change" onClick={this.handleDelete}>Supprimer</Button>

                            </div>
                        )}
                    </Container>
                </div>
            )
        }
    }

Each time I push Delete button I have this error : TypeError: Cannot read property 'number' of undefined.

Can someone tell my what I did wrong? Or what I need to had to have all informations needed? Thank you

3 Answers

it should be:

const id = this.state.data.number

not

const id = this.data.number

A good tip to find this in future is to always trace back from what is undefined to find the last thing that is defined and work from there

If you are getting the number from state , you should do this.state.number

Now in your code, the undefined is caused by :

1- this.setState({ error: errorThrown }) is overriding the state object and removing the number field

Or

2- You didn't initialize the state object correctly by not putting a number field

Or

3- this.state.number has not been set or has been set to another undefined variable

The best thing to do is to print out this.state before every time you access it. So the last printed state before the undefined error will tell what are the fields present in the state. If there is no number field then the error is caused by 1 or 2 If there is a number field but it has no value (undefined) then the error is caused by 3

There are no property data in constructor. Add data state at constructor. And then replace const id = this.data.number with const id = this.state.data.number

Related