Im running a react app with Firebase in the back. I have an endpoint "/items".
When I invoke a DELETE request through Axios, the database reflects the correct data. However, after the DELETE request when I invoke a GET request to retrieve updated "items" I get a null reference where the deleted item was, leading to a null reference error.
EXAMPLE
-Original [item1, item2, item3]
*Delete item2
-Get
- [item1, null, item3]
However on Firebase it is reflected correctly..
class VendingMachine extends Component {
state = {
balance: "",
items: [],
}
getItems = () => {
axios.get('items.json')
.then(response => {
if (response.data) {
this.setState({ items: Object.values(response.data) });
}
})
}
deleteItemHandler = (id) => {
axios.delete('/items/' + id + '.json')
.then((response) => {
this.getItems();
});
}
Put Request is in another component..
class NewItem extends Component {
errorElement = React.createRef();
state = {
newItem: {
name: "",
position: null,
price: "",
image: ""
},
showError: false
};
addItemHandler = () => {
if (this.props.items.length === 9) {
this.setState({ showError: true });
return;
}
const newItem = {
id: this.props.items.length,
name: this.state.newItem.name,
price: this.state.newItem.price,
image: this.state.newItem.image,
position: this.props.items.length
}
console.log(newItem);
axios.put('/items/' + newItem.id + '.json', newItem)
.then(response => {
this.props.updateItems()
})
.catch(error => console.log(error));
}