簡體   English   中英

通過 axios 帖子將 null 值發送到 sql

[英]Send null value to sql via axios post

我有一個非常簡單的問題。 我想使用 axios.post 方法將 null 值發送到 MySql 服務器。 該值在 sql 表中定義為 int。 我試過通過 null 但沒有任何反應。 我也嘗試過傳遞 '' 但最終得到 0。我驗證了任何隨機 int 都可以通過傳遞隨機整數來正常工作。 不過,我真的需要將其設為 null。 我怎樣才能做到這一點? 這什么也沒發送...

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

null 查詢時需要包裹在數組 [] 中。 所以換這個...

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

有了這個...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM