i want to check either username exist in mongo db or not i want to do it by promise , i am new in node.js please help me to understand actual scenario thanks in advance.
var errorsArr = [];
var promise = username();
promise.then(function(data){
errorsArr.push({"msg":"Username already been taken."});
},console.error);
username(function(err,data){
User.findOne({"username":req.body.username},function(err,user) {
if(err)
return console.error(err);
return user;
});
});
console.log(errorsArr);
Mongoose is already promisified, so this will do:
A Promise is asynchronous so only return the Promise and the caller will "wait" for it to be resolved and handle the errors.