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.

3
  • 3
    Two return's - now that's interesting. Commented Sep 27, 2016 at 10:41
  • Which message you want to send?I can't see any message you have define Commented Sep 27, 2016 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. Commented Sep 27, 2016 at 10:50

3 Answers 3

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
    });
  });
Sign up to request clarification or add additional context in comments.

Comments

1

Message is not defined in your code

 res.json({ result: 'An e-mail has been sent to ' + maillist + ' with further instructions'})

5 Comments

It showing error like this. How to define and send.
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');
whats the message supposed to say?
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.
cool. replace this " return res.json({result:message});" with return res.json({ result: "'An e-mail has been sent to ' + maillist + ' with further instructions"})
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`});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.