我想删除“待办事项”列表中的所有已完成任务。 在代码中,我将所有完成的任务分配给变量“ completed”。接下来,我想使用axios从服务器中删除已完成的任务。
removeItems = (event) => {
event.preventDefault();
let completed = [];
this.setState(prevState => {
return {
todos: prevState.todos.filter(todo => {
if (todo.completed == true) {
completed.push(todo)
}
return !todo.completed;
})
}
})
console.log(completed);
const remove = completed.map(async (todo) => {
await Axios.delete(`http://localhost:8000/todoes_destroy/{id}/`)
})
}
答案 0 :(得分:0)
我更改了代码,现在可以使用了。这是解决方案,
removeItems = async (event) => {
event.preventDefault();
const completed = this.state.todos.filter(todo => {
return todo.completed
});
const remove = completed.map( async (todo) => {
console.log(todo);
await Axios.delete(`http://localhost:8000/todoes_destroy/${todo.id}/`)
}
)
const {data} = await Axios.get('http://127.0.0.1:8000/todoes_read/', {
})
this.setState({todos : data})
}