简体   繁体   English

通过 axios 帖子将 null 值发送到 sql

[英]Send null value to sql via axios post

I have a very simple question.我有一个非常简单的问题。 I want to send null value to MySql server using axios.post method.我想使用 axios.post 方法将 null 值发送到 MySql 服务器。 The value is defined as an int in the sql table.该值在 sql 表中定义为 int。 I have tried passing null but nothing happens.我试过通过 null 但没有任何反应。 I have also tried passing '' but I end up with 0. I validated that any random int will work by passing random integers with no problem.我也尝试过传递 '' 但最终得到 0。我验证了任何随机 int 都可以通过传递随机整数来正常工作。 I really need to make it null though.不过,我真的需要将其设为 null。 How can I do this?我怎样才能做到这一点? This sends nothing...这什么也没发送...

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

This sends 0...这发送 0...

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

This sends 100...这发送了 100...

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

I need it to change an int back to null by literally sending null as a value.我需要它通过将 null 作为值发送来将 int 更改回 null。

server side code...服务器端代码...

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);
    })

null needs to be wrapped in an array [] when queried. null 查询时需要包裹在数组 [] 中。 so replace this...所以换这个...

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

with this...有了这个...

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

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

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