如何在 .js 文件中将 app.post 中的 NaN 值转换为 0
How to convert NaN value to 0 inside app.post in .js file
在下面的代码中,question1 的值是 NaN
。我想将它转换为 0,但我无法使用 parseInt
进行转换。我该怎么做?
app.post('/auth/total',function(req,res){
console.log(req.body);
const { question1, question2, question3, question4} = req.body;
let total1 = parseInt(question1) + parseInt(question2)
let total2 = parseInt(question3) + parseInt(question4)
//some code to insert data in db and render the view
});
你可以这样做:
let total1 = (parseInt(question1) || 0) + (parseInt(question2) || 0)
这会将任何虚假值转换为 0
在下面的代码中,question1 的值是 NaN
。我想将它转换为 0,但我无法使用 parseInt
进行转换。我该怎么做?
app.post('/auth/total',function(req,res){
console.log(req.body);
const { question1, question2, question3, question4} = req.body;
let total1 = parseInt(question1) + parseInt(question2)
let total2 = parseInt(question3) + parseInt(question4)
//some code to insert data in db and render the view
});
你可以这样做:
let total1 = (parseInt(question1) || 0) + (parseInt(question2) || 0)
这会将任何虚假值转换为 0