首页 > 解决方案 > 通过 axios post 向 sql 发送空值

问题描述

我有一个非常简单的问题。我想使用 axios.post 方法将空值发送到 MySql 服务器。该值在 sql 表中定义为 int。我试过传递 null 但没有任何反应。我也尝试过传递 '' 但最终得到 0。我验证了任何随机 int 都可以通过传递随机整数来正常工作。我真的需要让它为空。我怎样才能做到这一点?这什么也没发送...

    axios.post("http://localhost:3001/api/update-air", 
    {airValue: null}).then(() => {
        alert("Successfull")
    })

这发送 0...

    axios.post("http://localhost:3001/api/update-air", 
    {airValue: ''}).then(() => {
        alert("Successfull")
    })

这发送了 100...

    axios.post("http://localhost:3001/api/update-air", 
    {airValue: 100}).then(() => {
        alert("Successfull")
    })

我需要它通过将 null 作为值发送来将 int 更改回 null。

服务器端代码...

app.post("/api/update-air", (req, res) => {
    const airValue = req.body.airValue;
    const sqlUpdateAir = "UPDATE user SET airValue=(?) WHERE uid='j' ";
    db.query(sqlUpdateAir, (airValue), (err, result)=>{
        console.log(airValue);
    })

标签: javascriptmysqlnode.jsaxios

解决方案


查询时需要将 null 包装在数组 [] 中。所以换这个...

db.query(sqlUpdateAir, null, (err, result)=>{
    console.log(airValue);
})

有了这个...

db.query(sqlUpdateAir, [null], (err, result)=>{
    console.log(airValue);
})

推荐阅读