I am trying to process a request that needs a mail to be sent. In order to respond to the request without waiting for the mail to be sent i am sending the mail on an afterRemote hook. The method seems to run correctly and the mail is sent, but for some reason the cb function is not executed and as a result on the client the request remains unanswered. The problem is on the code that follows, as you can see i have console.log("Here");cb(null,{});
there and the first command gets executed, but not the second as it seems.
user.joinEntity = function(data, cb) {
var loopbackCtx = user.app.loopback.getCurrentContext();
var userId=loopbackCtx.accessToken.userId;
if(false){
cb( new Error("Cant join that Entity."),{});
}else{
user.find({where:{id:userId}},function(err,applicant_instance){
if (err) cb(err,{});
if(applicant_instance.length>0)
user.find({where:{id:data.ownerId}},function(err,founder_instance){
if (err) cb(err,{});
if(founder_instance.length>0)
user.app.models.EntityApplication.create({email:applicant_instance[0].email,userId:userId,EntityFounder:founder_instance[0].id,Entity:data.id,Status:"pending"},function(err,Entity_Application_Instance){
if (err) cb(err,{});
loopbackCtx.join_entity={applicant:applicant_instance[0].email,entity:data.name,to:founder_instance[0].email};
console.log("Here");
cb(null,{});
});
});
})
}
}
user.afterRemote('joinEntity',function(){
var loopbackCtx = user.app.loopback.getCurrentContext();
user.app.models.Email.send({
to: loopbackCtx.join_entity.to,
from: 'mailer@domain.org',
subject: 'Application to enter your Entity',
// text: '<strong>HTML</strong> tags are not converted'
html: 'User <strong>'+loopbackCtx.join_entity.applicant+'</strong> wants to enter Entity by the name of <strong>'+loopbackCtx.join_entity.entity+'</strong>'
}, function(err) {
if (err) throw err;
console.log('> email sent successfully');
});
});