Can someone find the bug in my code? I'm trying to make a delete button work that delete a specific challenge that has been clicked.
The error in the console:
xhr.js:178 DELETE http://localhost:3000/allchallenges/5eb4398f2650390017030bcf 404 (Not Found)
Front-end component that has a delete button
import React from 'react'
import DefaultLayout from "../layout/Default"
import Challengebox from '../components/Challengebox'
import axios from "axios";
class Allchallenges extends React.Component {
constructor() {
super()
this.state = {
challenges: []
}
this.onDelete=this.onDelete.bind(this)
}
componentDidMount(){
axios({
method: "GET",
url: `${process.env.REACT_APP_API_BASE}/allchallenges`,
withCredentials: true
})
.then(response => {
console.log(response)
let challengeslist = response.data;
this.setState({challenges: challengeslist})
})
.catch(error => {
console.log("You've made an error charles: ",error)
})
}
onDelete(challengeId){
axios
.delete(`${process.env.REACT_APP_API_BASE}/allchallenges/${challengeId}`)
.then(response => {
this.props.history.push("/allchallenges")
})
.catch(err => console.log(err))
}
render() {
return (
<DefaultLayout>
<div className="challengeoverviewlist">
<h1>All challenges</h1>
<div className="challengeboxes">
{
this.state.challenges.map(challenge =>
(
<div className="totalbox" key={challenge._id}>
<Challengebox
key={challenge._id}
id={challenge._id}
title={challenge.title}
description={challenge.description}
/>
<button onClick={() => this.onDelete(challenge._id)}>
Delete
</button>
</div>
))
}
</div>
</div>
</DefaultLayout>
)
}
}
export default Allchallenges
My api:
//request challenges
router.get("/allchallenges", (req,res) => {
Challenge
.find()
.then(response => {
res.json(response)
})
.catch(error => {
res.json(error)
})
})
//delete challenge
router.delete("/allchallenges", (req,res) => {
Challenge
.find()
.then(response => {
res.json(response)
})
.catch(error => {
res.json(error)
})
})