1

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');       
      });
  });
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
Evan
  • 1,683
  • 7
  • 35
  • 65
  • Hey @Evan, are you getting an error message? And the client just hangs? One thing you might try is `console.log(cb)` to be sure your callback exists. I don't see any reason it wouldn't, but it's a decent sanity check. - Brennan – Brennan Apr 08 '15 at 02:33
  • @BrennanPayne Hey, there is no error and the callback seems to be defined. This is very weird >:( – Evan Apr 08 '15 at 09:51
  • Interesting. It's possible that the callback is actually being called? Do you expect the `user.afterRemote('joinEntity' ...` to be called after your this callback. Have you debugged to be sure the issue isn't in that method? – Brennan Apr 08 '15 at 19:42

1 Answers1

1

Typically you'll want to be sure to execute the next() callback on a remote hook to tell LB (or rather Express) that you want this particular piece of middleware to allow processing to continue. If you don't call next() then you're basically telling Express that this is the end of middleware processing. Make sure you accept all three arguments to the remote hook, then execute next() when the method action is complete:

user.afterRemote('joinEntity',function(ctx, instance, next){
  // *** notice arguments above!

  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) {
    // *** notice the correct way to indicate an error!
    if (err) { next(err); }

    console.log('> email sent successfully');

    // *** Now tell Express to keep processing middleware
    next();
  });
});
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55