创建新任务时todo删除/放置请求不起作用

问题描述 投票:0回答:1

我正在学习后端开发,需要 mongodb、axios 方面的帮助

我的项目是一个简单的待办事项应用程序,它将创建的任务存储在 mongoDB 数据库中。

存储库的链接是这里

我的问题是,当我创建任务时,我无法将其标记为已完成或将其删除,直到刷新页面。

我检查了后端服务器错误并发现了附加的突出显示的错误

我还检查了前端服务器,发现 axios 错误如附件突出显示

通过调查,我意识到 ObjectId 期望接收十六进制字符串或整数作为参数,但不知何故它从 axios (未定义)接收错误日志中指示的其他内容,而不是任务 id。

我该如何解决这个问题?

node.js mongodb vue.js axios mongodb-query
1个回答
0
投票

问题是,创建任务时,将其添加到

todos
数组中,因此缺少
_id
,传递到服务器时未定义,给出错误:

    await axios.post('http://localhost:5200/', {
        title: this.title,
        duration: this.duration,
        priority: this.priority,
        description: this.description,
        status: this.task_done
    })
        .then(
            this.todos.push({
                title: this.title,
                duration: this.duration,
                priority: this.priority,
                description: this.description,
                status: this.task_done
            })
        )

https://github.com/HanyTaha61/MEVN-stack-project/blob/master/src/components/todo-form.vue#L118

服务器返回新创建的待办事项,因此只需添加从服务器返回的待办事项即可。

尝试将上面的内容更改为:

    axios.post('http://localhost:5200/', {
            title: this.title,
            duration: this.duration,
            priority: this.priority,
            description: this.description,
            status: this.task_done
        })
        // add fresh todo, which has _id and all
        .then(newTodo => {

            this.todos.push(newTodo);

        })

此外,不需要

await
,因为您使用了
.then
,所以您可以在整个过程中更改它

© www.soinside.com 2019 - 2024. All rights reserved.