为什么axios不会从变量中删除元素?

时间:2019-07-05 08:15:02

标签: javascript axios

我想删除“待办事项”列表中的所有已完成任务。 在代码中,我将所有完成的任务分配给变量“ 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}/`)
    })
}

1 个答案:

答案 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})

    }