I am trying to return found entry from a Mongoose entry on my collection into a variable which the function will return after few more checks.
The current code is getting the document, but I need to access it and return it at the end. Is it possible to assign a return query outside the callback?
The current function:
login_user(username, psw) {
let hashedPsw = _SECURITY.string_to_sha1(psw);
let userObject = {};
webkbuser.findOne({'user_username': username}).exec(function(err, ret) {
if (err) return _UTILS.errorHanlder(err, false, true, null);
userObject = ret;
});
console.log(userObject);
}
As predicted the console.log returns a '{}' output since nothing is getting assigned to it.
The object is being created in a router and login is being called in if statement atm
router.post('/', function(req, res, next) {
if (!req.body) return res.sendStatu(400);
if (!req.body.usr) return res.sendStatus(400);
if (!req.body.psw) return res.sendStatus(400);
let user = new _USER('LOGIN');
if (user.login(req.body.usr, req.body.psw) === false) return res.sendStatus(401);
req.session.active = true;
res.sendStatus(200);
})