紛らわしいタイトルで申し訳ありませんが、説明を試みます
Express-formとカスタム検証関数でnode.js を次のように使用しています。
app.post('/register', form(
field("username").trim().required().custom(function(value) {
//Works correctly
if (!user.validUsername(value)) {
throw new error("Invalid username");
}
//Does not work due to callback
user.uniqueUsername(value, function(uniqueUsername) {
if (!uniqueUsername) {
//Not unique, throw error
throw new Error("username is already taken");
//Could be subsituted for return "whatever"; and it would still be the same problem
}
});
}),
registerAttempt);
};
// ...
//Example function for brevity
user.uniqueUsername = function(username, callback) {
User.findOne({username: username}, function(err, user) {
if (err) { throw err; }
callback(user === null);
});
}
.custom(function(value) { .. }) がコールバックを受け取るまで実行を終了しないように、これを再構築する方法が必要ですが、どうすればそれができるかわかりません。
編集:エラーを修正