Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Axios回调函数在put方法中不起作用_Node.js_Reactjs_Express_Axios_Response - Fatal编程技术网

Node.js Axios回调函数在put方法中不起作用

Node.js Axios回调函数在put方法中不起作用,node.js,reactjs,express,axios,response,Node.js,Reactjs,Express,Axios,Response,我正在尝试用Axios更新MySQL数据库。我的问题是数据更新成功,但axios回调不工作。而post、delete和get回调都能正常工作。下面是我的代码: 前端代码: UpdateTeacher = (id) =>{ const editedata = { id: id, fname: this.state.fname, lname: this.state.lname, roles: this.state.r

我正在尝试用Axios更新MySQL数据库。我的问题是数据更新成功,但axios回调不工作。而post、delete和get回调都能正常工作。下面是我的代码:

前端代码:

 UpdateTeacher = (id) =>{
      const editedata = {
        id: id,
        fname: this.state.fname,
        lname: this.state.lname,
        roles: this.state.roles,
        join_date: this.state.join_date,
        email: this.state.email,
        assign_subject: this.state.assign_subject
      };
    
     axios.put(`http://localhost:3001/edit/${id}`, editedata)
      .then((res) => {
        // I cant get any console.log neither any state set here
        this.setState({
          success: "Update data Successfully!", 
          type:"success",
          isEdit:false
        });
       
        console.log(res.data.json);
      }).catch((err) => {
        console.log(err);
        this.setState({ 
            error: "Error! Please try again later!", 
            type:"error"
        });
      })
  }
后端代码:

app.put("/edit/:id", (req, res) => {

    const {id }       = req.params.id;
    const fname     = req.body.fname;
    const lname     = req.body.lname;
    const roles     = req.body.roles;
    const joining   = req.body.join_date;
    const email     = req.body.email;
    const assign_subject  = req.body.assign_subject;
    
   var updateData = req.body;
   console.log(updateData);
 
db.query(`UPDATE teacher SET fname=?, lname=?, roles=?, join_date=?, email=?, assign_subject=? WHERE id=?`,[fname, lname, roles,joining,email,assign_subject, req.params.id]),(err,result)=>{

    if (err) return console.error(err.message);
    res.status(200).send(results);
    console.log("Updated Row(s):", results.affectedRows);
}
});

这里有一些问题可能会导致问题。在服务器代码中,如果出现错误,必须返回响应,否则服务器将挂起

大概是这样的:

if(err)返回res.status(500).send(err.message);
此外,您正在发送
结果
,但在
db.query
回调中声明的变量是
result
。所以代码应该是

res.status(200).发送(结果);
log(“更新的行):”,result.affectedRows);

第三,在您的客户机代码中,您使用
console.log(res.data.json)
,但我认为这应该是
console.log(res.data)

谢谢你的快速回答。作为您的回答,我更改了变量,但没有运气,状态仍然是先“挂起”,然后有时失败。这是新代码
db.query(
UPDATE teacher SET fname=?,lname=?,roles=?,join_date=?,email=?,assign_subject=?WHERE id=?
,[fname,lname,roles,join,email,assign_subject,req.params.id]),(err res res status(500)。send(err.message);res.status(200)。send(result);console.log(“更新的行):”,result.affectedRows);}})@B.M.rafiulam我在答案中添加了另一个问题。看看这是否有帮助。如果没有,能否显示服务器日志的输出?还可以尝试在客户端日志之前放置一个控制台日志,其中包含一个简单的字符串,如“-----------”。如果传递给console.log()的值未定义或为null,则通常不会显示任何内容。最后,我发现主要问题出现在MySQL查询中。我不知道确切的位置!我的新工作代码在服务器端:
db.query('UPDATE
teacher`SET?WHERE id=?',[req.body,req.params.id],函数(err,result){if(err){console.log(err);}else{res send(result);};})`