콜백을 이해하는 몇 가지 문제가, heres는노드 JS 콜백
function authenticate(username, password, callback) {
var query = "SELECT * from mydb.users where username='" + username + "'and BINARY password='" + password + "'";
connection.query(query, function (err, rows) {
if (rows[0] != undefined) {
if ('username' in rows[0]) {
if (rows[0].username == username) {
callback(true);
}
}
}
callback(false);
});
};
exports.assets=function(req,res) {
var username = req.param("username");
var password = req.param("password");
authenticate(username, password, function (didAuthenticate) {
if (didAuthenticate) {
console.log("Authenticated");
}
else {
console.log("Failed");
}
});
};
출력이 때 사용자/일치 통과 (MySQL을 통해) 인증을 위해 내 코드 :
Failed
: 없음 일치로
Authenticated
Failed
을
didAuthenticate가 true를 반환 할 때 if/else 문이 작동하지 않는 이유는 무엇입니까?
+1 처음 보았을 때 흔히 발생하지만 약간 혼란스러운 패턴입니다. 'returns'는 값을 반환하기 위해 _not_ 거기에 있지 않습니다 (거의 아무도 듣고 있지 않습니다), _flow control_ 만 있습니다. – user949300
완벽. 감사! – user3217789
@ user3217789 : 문제 없습니다! 귀하의 질문에 [답변 수락] (http://stackoverflow.com/help/someone-answers)을 기억하십시오. –