我是NODE的新手。我建立了一个POST API来执行选择查询操作。我编写了一个执行选择查询操作的函数。 如果用户存在,我将向调用函数发送1或发送0。基于响应,我试图发送自定义响应。但是我要未定义。
下面是我的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"
});
}
});
下面是我的功能
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
}