Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  22] [ 6]  / answers: 1 / hits: 34175  / 9 Years ago, tue, january 19, 2016, 12:00:00

I want to check my data is blank or undefined but my if block execute even my data is not blank ...



Code is :



router.post('/addNewGrade',function(req , res){ 
var errorMsg = [];
console.log(req.body.gradeName)
if(req.body.gradeName == '' || req.body.gradeName === undefined){
errorMsg.push(please enter grade name);
}
if(req.body.gradeDescription == '' || req.body.gradeDescription === undefined){
errorMsg.push(please enter description about your grade);
}
if(errorMsg !=''){
res.send({errorMessage :errorMsg});
return;
}

});


what is the best way to check variable is undefined or not


More From » node.js

 Answers
29

Because an undefined variable is falsey, you can simple do



if (body.req.gradeName) {
// do normal stuff
} else {
// do error stuff
}


Or if you don't need to do anything if it is defined, then you can do



if (!(body.req.gradeName)) {
// do error stuff
}

[#63654] Sunday, January 17, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
micheleb

Total Points: 275
Total Questions: 103
Total Answers: 103

Location: Macau
Member since Sat, Jul 11, 2020
4 Years ago
;