-1
我想檢查用戶名是否存在。 我必須同步運行它。使用異步包同步運行貓鼬查詢
function notExisted(string) {
var username = '';
//STEP 1: check if any user is existed with the givven username, and asign a value to username var.
User.findOne({'username': string}, function (err, result) {
if (err) {
req.flash('error', 'An error occured.');
res.redirect("back");
} else {
if (!result === null) {
username = result.username;
} else {
username = null;
}
}
});
// STEP 2: based on username varibale return false(if founded) or true(if not founded)
// if any user has founded, the username variable would be the username. Otherwise it would be null.
if (username === null) {
return true;
} else {
return false;
}
}
正如你看到的步驟1和2應該一個接一個地運行。 你知道如何通過async庫或其他更好的方法同步運行這2步嗎? 在此先感謝。
_「我必須同步運行」 _。爲什麼?簡答:你不能。 – robertklep