5
I am using the Sequelize.js. framework to perform queries in the database, the result of the function find
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 make 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 as return that the object does not have a method success
the idea is 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 parameters of the restify
to answer the Rest call
How to return an object, asynchronously, with methods after the execution of the query, in nodejs?
I changed the title. After your last post I saw that the sequelize really uses Promises.
– Caputo
Was there anything left to explain? I didn’t quite understand the reward!
– bfavaretto
I tried to find an explanation of how to use the predecessors myself, so I wanted more visibility in the question. But then I found an online course on the site http://nodeschool.io/. But still your answer helped me solve the problem. Thank you!
– Caputo