[英]Node.js express add default value mysql
所以我有帶有post方法的nodejs api。 我收到錯誤查詢,因為我沒有在total_post上輸入數據。
這是我的代碼:
var db = require('./../db');
module.exports.jumlah=function(req,res){
var data={
"id_account":req.body.id_account,
"name":req.body.name,
"address":req.body.address,
"total_post":req.body.total_post
}
console.log(data);
db.query('INSERT INTO t_jumlah SET ?',data, function (error, results, fields) {
if (error) {
res.json({
status:false,
message:'there are some error with query'
})
}else{
res.json({
status:true,
message:'data inserted sucessfully'
})
}
});
}
我想如果不在total_post字段中輸入數據,它將自動輸入0值。 我該如何實現?
這個怎么樣?
"total_post":req.body.total_post || 0
如果total_post是undefined或false,它將回落到0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.