i'm new to node js, i'm using restify and mongoose. so, i try to return some value when i found the data and return null when data not found. here is my code:
getUsers = function (user, pass, urole){
var data = '';
Publisher.findOne({username:user, password:pass, role:urole},function(err, success){
if (err){}
if (!success){
//when data not found
data = 'null';
}else{
//when data found
data = 'found';
}
});
return data;
}
and i want to call the function and assign to the variable.
var user = getUsers(user,pass,urole)
console.log(user)
but the result is undefined.
The findOne
is async function, please put return data
into the callback function, also return date through callback function
getUsers = function (user, pass, urole, callback){
var data = '';
Publisher.findOne({username:user, password:pass, role:urole},function(err, success){
if (err){}
if (!success){
//when data not found
data = 'null';
}else{
//when data found
data = 'found';
}
callback && callback(data);
});
}
getUsers(user, pass, urole, function(data) {
console.log(data);
});