【发布时间】:2018-06-25 11:38:52
【问题描述】:
所以我有带有 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 -
@akshaygore 它的工作。谢谢