gpt4 book ai didi

angularjs - 如何从node.js函数向 Angular Controller 发送消息

转载 作者:太空宇宙 更新时间:2023-11-04 03:29:43 24 4
gpt4 key购买 nike

我在node.js函数中有一个名为message的变量,现在我想将它发送到 Angular Controller 。如何做到这一点?

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 的错误。

最佳答案

尝试在最后一次回调中执行

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

关于angularjs - 如何从node.js函数向 Angular Controller 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39722490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com