【发布时间】:2023-04-08 08:56:01
【问题描述】:
我想检查用户名是否存在。 我必须同步运行它。
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 库或任何更好的方法同步运行这两个步骤吗? 提前致谢。
【问题讨论】:
-
“我必须同步运行”。为什么?简短的回答:你不能。