0

I'm trying to build a custom validator for my express application. Here it is

app.use(validator({
    customValidators:{
        isAvalible:function(userName){
            console.log("userName as param = " + userName)
            //if username is not present in database return True else False
            var query = user.where({userName:userName})
            query.findOne(function(err,user){
                if(err){ 
                    console.log("error occured in findOne()")
                    return handleError(err)
                }
                if(user){
                    console.log("user = " + user)
                    if(user == null){
                        return true
                    }
                    else{
                        return false
                    }

                }
            })
            console.log("returning")
            return true;
        }
    }
}));  

It's functionality is to check if a userName inserted into a registration form is alredy in use by some other user.
Query.findOne() is an async function i belive, thus the above function logs me "userName" and immediatly after "returning". It dosent even enter the findOne()'s callback, thus results are not set properly. How could i fix this behaviour?

  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Yury Tarabanko Mar 29 '17 at 12:20
  • @YuryTarabanko Indeed it is a duplicate. Thanks for noticing. Can u close it or shall i delete it? –  Mar 29 '17 at 13:33
  • I have already voted to close. We need 4 members more to think so. :) Or someone who has golden badge on any of tags :) – Yury Tarabanko Mar 29 '17 at 15:21

0 Answers0