繁体   English   中英

如何使用axios API将带有Vue的前端数据发送到带有nodeJs的后端,以获得CRUD中的更新功能?

[英]How to send data from the frontend with Vue to backend with nodeJs for the update functionality in CRUD using axios API?

我能够完成除更新之外的CRUD操作,我的猜测是进入updateTask函数的数据流没有正确发生,我需要做些什么修正才能解决问题?

我以id和标题的形式将数据发送到updateTodo函数,但它不起作用。

此代码位于前端文件中。

 updateTodo(id,title) {
      axios.put(`http://localhost:4000/todos/${id}`,{title:this.todo.title}).then(() => {
        this.getTodos();
        this.isEditing = false;
      });
    }

这是与axios链接的后端代码。

router.route('/:id').put((req,res)=>{
    Todo.findById(req.params.id,(err,todo)=>{
        if(!todo){
            res.send('Error fetching the todo.');
        }else{
            todo.title = req.body.title;
            todo.save()
            .then(todo=>{
                res.json({'msg':'Todo updated successfully.'});
            })
            .catch(err=>{
                res.send('the error in updating the task is ' + err);
            })


        }
    })
})

更新功能在后端使用put方法(通过邮递员检查)正常工作,但不能从前端工作。

updateTodo(todo) {
  axios.put(`http://localhost:4000/todos/${todo._id}`,{title:todo.title}).then(() => {
    this.getTodos();
    this.isEditing = false;
  });
}

分别发送整个todo而不是id和title,我不好意思在发布之前不检查它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM