如何从node.js函数向角度控制器发送消息

时间:2016-09-27 10:39:20

标签: angularjs node.js

我在node.js函数中有一个名为message的变量,现在我想把它发送到角度控制器。怎么做?

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});
});

我尝试发送return res.json({result:message});,但它显示了一个名为message undefined的错误。

3 个答案:

答案 0 :(得分:2)

尝试进行上一次回调

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();
      });

并在最后的回调中

  ], function(err,result) {
    if (err) return next(err);
    return res.json({
      result: result.message
    });
  });

答案 1 :(得分:1)

您的代码中未定义消息

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

答案 2 :(得分:0)

消息是一个未定义的变量...要么定义它,要么传递一个字符串或者更好地使用template literals ---> ES6!

此刻你发出一堆异步函数,但在回复前端之前没有等待其中任何一个返回。

Using Async waterfall in node.js

我假设你一旦完成异步功能的工作,你想传回一条消息?

如果没有,你只需发送信息......

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

根据上述评论......

return res.json({result:`An e-mail has been sent to ${maillist} with further instructions`});