0

I have a variable called message in node.js function now I want to send it to angular controller. How to do this?

api.js

router.post('/pages/auth/forgot-password', function(req,res,next){
    var maillist = req.body.email;
    async.waterfall([
        function(done) {
            crypto.randomBytes(20, function(err, buf) {
                var token = buf.toString('hex');
                done(err, token);
            });
        },

        function(token, done) {
            User.findOne({ email : maillist}, function(err, user) {
                if (!user){
                    return done(null, false,{message: 'No account with that email address exists.'});
                    return res.redirect('/pages/auth/forgot-password');
                } 
                
                user.resetPasswordToken = token;
                user.resetPasswordExpires = Date.now() + 3600000;           
      
                user.save(function(err) {
                    done(err, token, user);
                });
                
            });
        },
        
        function(token, user, done) {
            var mailOptions={
                to : maillist,
                subject : 'Password Recovery',                  
                text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
                'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
                'http://192.127.0.1:3000/pages/auth/reset-password/' + token + '\n\n' +
                'If you did not request this, please ignore this email and your password will remain unchanged.\n'
            };
            transport.sendMail(mailOptions, function(error, response){
                if(error){
                    return done(null, false,{message: 'An e-mail has been sent to ' + maillist + ' with further instructions.'});
                }
                transport.close();
            });
            
        }
    ], function(err){
        if (err) return next(err);
        res.redirect('/pages/auth/forgot-password');
    });
    return res.json({result:message});
});

I am trying to send like return res.json({result:message}); But it shows an error called message undefined.

Community
  • 1
  • 1
Kevin
  • 653
  • 2
  • 13
  • 34
  • 3
    Two `return`'s - now that's interesting. – Zlatan Omerović Sep 27 '16 at 10:41
  • Which `message` you want to send?I can't see any message you have define – abdulbarik Sep 27 '16 at 10:43
  • I have edited my question. I want to send any one of these ` return done(null, false,{message: 'No account with that email address exists.'});` or `return done(null, false,{message: 'An e-mail has been sent to ' + maillist + ' with further instructions.'});`. If user not found that time I want to send first one and if it is there after sent mail I want to send second one. – Kevin Sep 27 '16 at 10:50

3 Answers3

2

Try to do in your last callback

transport.sendMail(mailOptions, function(error, response) {
        if (!error) {
          var message = {
            message: 'An e-mail has been sent to ' + maillist + ' with further instructions.'
          };
          done(null, message);
        }
        transport.close();
      });

And in final callback

  ], function(err,result) {
    if (err) return next(err);
    return res.json({
      result: result.message
    });
  });
abdulbarik
  • 6,101
  • 5
  • 38
  • 59
1

Message is not defined in your code

 res.json({ result: 'An e-mail has been sent to ' + maillist + ' with further instructions'})
Deepak
  • 1,373
  • 2
  • 10
  • 31
  • It showing error like this. How to define and send. – Kevin Sep 27 '16 at 10:54
  • There are more issue in your code. For example: after return javascript execution will stop but you doing some other activities after return function. Note: return done(null, false,{message: 'No account with that email address exists.'}); return res.redirect('/pages/auth/forgot-password'); – Deepak Sep 27 '16 at 10:55
  • whats the message supposed to say? – Sten Muchow Sep 27 '16 at 10:55
  • Email was successfully sent. so now I need to display on my page this message `message: 'An e-mail has been sent to ' + maillist + ' with further instructions.` For this first I want send this message to controller. – Kevin Sep 27 '16 at 10:57
  • cool. replace this " return res.json({result:message});" with return res.json({ result: "'An e-mail has been sent to ' + maillist + ' with further instructions"}) – Deepak Sep 27 '16 at 10:59
0

Message is an undefined variable... Either define it or pass a string or better yet use template literals ---> es6!

At the moment u r firing a bunch of async functions but not waiting for any of them to return before you reply to the frontend.

Using Async waterfall in node.js

I assume once yuo finish the work of the async funciton you want to pass a message back?

If not and u just wana send a message...

return res.json({result:'I am a message string....'});

Based on the comment above...

return res.json({result:`An e-mail has been sent to ${maillist} with further instructions`});
Community
  • 1
  • 1
Sten Muchow
  • 6,623
  • 4
  • 36
  • 46