1

I'm new to NODE. I've built a post api to perform select query operation. I've written a function which performs select query operation. If user exists i'm sending 1 or sending 0 to the calling function. Based on the response i'm trying to send the custom response. BUT I'M GETTING UNDEFINED..??

Below is my api :

router.post('/registeruser',(req, res, next) => {
var username    =    req.body.username;
var email       =    req.body.email;
var password    =    req.body.password;

var checkuser   =    DbHandler.checkUser(username,email);   

 if(checkuser == 0) {
  console.log(checkuser); // i'm getting undefined
     console.log("insert user");
 } else if(checkuser == 1) {
  console.log(checkuser); // i'm getting undefined
    res.status(201).json({
             error   : true,
             message : "User with same credentials exist's"    
    });
 } 

});

below is my function

         checkUser(username,email) {
     //sql query
     dbConnection.query("SELECT * FROM users WHERE username = '"+username+"' AND email = '"+email+"'",function(error,result,fields){
        if(!!error){
            return error;
     } else {
        console.log(result);
        if(result == null || result.length == 0){
            console.log("inside if");
            return 0;// success response or user deos not exists confirmation
        } else {
            return 1;    
        }
     }//end of else
    });//end of query execution function   
} 

0 Answers0