我想做一些事情,比如當用戶搜索 mongoDB 資料庫,并且用戶存在時,它應該向用戶發送一封帶有密碼重置鏈接的電子郵件。由于某種原因,它沒有按預期作業。它只是繼續發送電子郵件,并且不檢查電子郵件是否存在,我不知道它為什么會這樣。
我的代碼如下所示:
exports.forgot_pass = function(req,res,next){
User.findOne({
user_email : req.body.user_email
}).exec((err,user)=>{
if(err){
res.status(500).send({message : err});
return;
}
if(!user){
res.status(400).send({message: "Sorry Email does not Exist!"});
}else{
var user_email = req.body.user_email;
const transporter = nodemailer.createTransport({
service:'gmail',
host: 'smtp.gmail.com',
port:'587',
auth:{
user: '**************@gmail.com',
pass: '***********'
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});
const mailOptions = {
from :'***********@gmail.com',
to: user_email,
subject: 'Please Reset your Password',
html : '<h3>Dear User</h3><p>You have requested to Reset your password. To Reset your password Successfully, Follow the Link bellow to Reset it</p><p>Click <a href="https://**********/user/resetPassword.jsp">https://onepercentsoft.oxygen.com/user/resetPassword.jsp</a></p><p>This Email is subject to mandatory instruction.</p><p>Regards,</p><p>Online Service</p>'
};
transporter.sendMail(mailOptions,function(error,info){
if(error)throw error;
return res.send({error:false, data: info, message: 'OK'});
})
}
});
};
但這不會檢查任何內容,它只是繼續發送電子郵件。我在這里需要幫助。
uj5u.com熱心網友回復:
嘗試使用async await. 問題可能與您如何處理承諾有關。
exports.forgot_pass = async function (req, res) {
try {
const { user_email } = req.body;
const user = await User.findOne({ user_email });
if (!user) {
return res.status(400).send({ message: 'Sorry Email does not Exist!' });
}
const transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port: '587',
auth: {
user: '**************@gmail.com',
pass: '***********',
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false,
},
});
const mailOptions = {
from: '***********@gmail.com',
to: user_email,
subject: 'Please Reset your Password',
html: '<h3>Dear User</h3><p>You have requested to Reset your password. To Reset your password Successfully, Follow the Link bellow to Reset it</p><p>Click <a href="https://**********/user/resetPassword.jsp">https://onepercentsoft.oxygen.com/user/resetPassword.jsp</a></p><p>This Email is subject to mandatory instruction.</p><p>Regards,</p><p>Online Service</p>',
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) throw error;
return res.send({ error: false, data: info, message: 'OK' });
});
} catch (err) {
res.status(500).send({ message: err });
}
};
uj5u.com熱心網友回復:
Sending email will take a little bit time. first of all you have to check is email exist. if exist then you have to send mail. for this purpose make your function asynchronous and us e await.
exports.forgot_pass = async function(req,res,next){
await User.findOne({
user_email : req.body.user_email
}).exec((err,user)=>{
if(err){
res.status(500).send({message : err});
return;
}
if(!user){
res.status(400).send({message: "Sorry Email does not Exist!"});
}else{
var user_email = req.body.user_email;
const transporter = nodemailer.createTransport({
service:'gmail',
host: 'smtp.gmail.com',
port:'587',
auth:{
user: '**************@gmail.com',
pass: '***********'
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});
const mailOptions = {
from :'***********@gmail.com',
to: user_email,
subject: 'Please Reset your Password',
html : '<h3>Dear User</h3><p>You have requested to Reset your password. To Reset your password Successfully, Follow the Link bellow to Reset it</p><p>Click <a href="https://**********/user/resetPassword.jsp">https://onepercentsoft.oxygen.com/user/resetPassword.jsp</a></p><p>This Email is subject to mandatory instruction.</p><p>Regards,</p><p>Online Service</p>'
};
transporter.sendMail(mailOptions,function(error,info){
if(error)throw error;
return res.send({error:false, data: info, message: 'OK'});
})
}
});
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/369276.html
