Node.js 为什么我的错误论点不算是错误的?
我的请求中有一个参数为false,但它没有被检测为false。。。我的代码如下所示:Node.js 为什么我的错误论点不算是错误的?,node.js,Node.js,我的请求中有一个参数为false,但它没有被检测为false。。。我的代码如下所示: router.post('/leave', (req, res) => { const myBooleanValue = req.body.myBooleanValue console.log(myBooleanValue) // This prints out 0 if (!myBooleanValue) { // This bit never gets call
router.post('/leave', (req, res) => {
const myBooleanValue = req.body.myBooleanValue
console.log(myBooleanValue) // This prints out 0
if (!myBooleanValue) {
// This bit never gets called
}
})
如您所见,
myBooleanValue
为0
,表示false。但是,if子句的内部代码永远不会被调用,因为它不会检测到它为false。我还尝试了if(myBooleanValue==0){}
和if(myBooleanValue==false){}
。但这不起作用。请帮忙?我猜myBooleanValue
的类型是字符串,而不是数字<代码>!“0”==false。如果(!(+myBooleanValue))我猜myBooleanValue
的类型是字符串,而不是数字,则可以执行类型强制<代码>!“0”==false
。如果(!(+myBooleanValue))首先确保它不会作为文本返回'0'
,则可以执行类型强制。如果它作为文本返回,则通过执行以下操作将其转换为int
const myBooleanValue = parseInt(req.body.myBooleanValue);
if (myBooleanValue === 0) {
// This bit never gets called
}
首先确保它不会以文本形式返回'0'
。如果它作为文本返回,则通过执行以下操作将其转换为int
const myBooleanValue = parseInt(req.body.myBooleanValue);
if (myBooleanValue === 0) {
// This bit never gets called
}
您是否尝试过parseInt(myBooleanValue)==0
很可能myBooleanValue
是字符串类型。(!“0”)不算作true如果您控制了客户端,请尝试发送false
而不是“0”
谢谢大家,你们都是对的!!您是否尝试过parseInt(myBooleanValue)==0
很可能myBooleanValue
是字符串类型。(!“0”)不算作true如果您控制了客户端,请尝试发送false
而不是“0”
谢谢大家,你们都是对的!!