【问题标题】:Node.js express add default value mysqlNode.js express 添加默认值 mysql
【发布时间】: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 它的工作。谢谢

标签: mysql node.js api


【解决方案1】:

这个怎么样?

"total_post":req.body.total_post || 0

如果 total_post 未定义或为 false,则返回 0

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 2016-08-03
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2016-12-20
    • 2023-03-25
    相关资源
    最近更新 更多