2
I started to stew Node.js
not long ago and I am using the division of logic into modules. I have a UserSessionController
validates whether 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 calling the code goes without receiving 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();
}
how this call is executed on 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 call Javascript
is executed in a manner synchronous?
It worked perfectly. I’m trying to learn this asymchronism paradigm, because I’ve always worked with imperative liguagens. Thank you
– Caputo