I'm using the Sequelize.js framework. to perform queries on the database, the result of the find
function returns an object with 3 methods: success
, error
and complete
Ex:
models.ItensPedido.findAll( {where : {PedidoId: req.params.pedidoId}})
.success( function( pedidos ) {
res.send(200, pedidos);
next;
})
.error( function(err) {
console.log("Erro ao listar itens do pedido", req.params, err);
res.send(500, "Erro ao listar itens do pedido: ", err);
next();
})
I tried to do a function that returned the same way, but without success
function validateLogin( pemail, ppwd ) {
return function () {
var successfull = false;
var error;
var user;
exports.success = function ( userSuccess ) {
if (!successful) retrun;
userSuccess(user);
}
exports.error = function ( userError ) {
if (!error) {retrun;}
userError(error);
}
models.User.find( { where: Sequelize.and({email: pemail}, {pwd: ppwd}) })
.success( function (foundUser) {
if (!user) {error = "User not found."; return;}
user = foundUser;
successfull = true;
})
error( function (err) {
error = err;
})
}
}
But I always have a return that the object does not have a method success
The idea is to be able to use it as follows:
validateLogin( "[email protected]", "123456")
.success( { //logica de sucesso } )
.error( { //logica de erro } );
Today I get the expected result as follows, passing the callbacks to the method:
validateLogin( "[email protected]", "123456", doOnSuccessLogin, doOnErrorLogin);
But for this I need the two callbacks to be declared within the same function by the closure of the restify
parameters to answer the call rest
How to return an object, asynchronously, with methods after the query execution, in nodejs?