I started porting Node.js
recently and I'm using the division of logic into modules. I have UserSessionController
that validates if the user session is still valid:
module.exports.getUserSession = function ( userToken ) {
models.UserSession.find( {where : Sequelize.and(
{ token: userToken }, { dataExpira { lte: Date() } }) } ).complete ( function (err, user) {
if (!!err) {
console.log('Ocorreu um erro ao buscar a sessão do usuário:', err);
return null;
} else {
return user;
};
})
}
But when I try to execute this call from another module, when making the call the code follows without receiving the return, because it will only be processed when the query is completed.
usuarioLogado = userSession.getUserSession(token)
if (!!usuarioLogado) { //aqui usuarioLogado está undefined
//faz a ação
} else {
res.send(401, "Você deve estar logado para realizar alterações.");
next();
}
As this call is executed in a webservice REST
the response to the WS client occurs before the query is executed in the database.
Is there a way to make a Javascript
call run in a synchronous way ?