Apologies for the confusing title, I will attempt to explain
I am using node.js with express-form and the custom validation function like so:
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);
});
}
I need a way to restructure this so the .custom(function(value) { .. }) doesn't finish executing until I have received the callback but I have no idea how I could do it, if at all.
EDIT: corrected error