javascript - 通过 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);
})
解决方案
查询时需要将 null 包装在数组 [] 中。所以换这个...
db.query(sqlUpdateAir, null, (err, result)=>{
console.log(airValue);
})
有了这个...
db.query(sqlUpdateAir, [null], (err, result)=>{
console.log(airValue);
})
推荐阅读
- docker - Windows 上的 Docker Linux 容器 - 无法确定链接层地址/拒绝使用非规范名称访问文件
- python - ImportError:无法从部分初始化的模块“bios.models”导入名称“Bio”(很可能是由于循环导入)
- r - 如何以 1 分钟为间隔创建 28 小时制表?
- javascript - CSS转换翻译不适用于javascript
- laravel - Amazon Elastic Beanstalk EC-2 自动为我的 Laravel 生产项目创建了一个备份文件
- c++ - fgets() 只读取最后一行
- javascript - sort() 在这种情况下如何工作?
- r - 在 R 中将 data.frame 切割成唯一的两半
- azure - 只有通过 terraform 添加资源会导致首先销毁它
- vb.net - 回车打印到文本 VB.net