实施忘记密码功能、搜索和如果电子邮件存在,发送电子邮件 Node.JS 无法按预期工作
Implementing forgot password feature, Search and if email exists, send email Node.JS does not work as Expected
我想做一些事情,比如当用户搜索 mongoDB 数据库并且用户存在时,它应该向用户发送一封电子邮件,其中包含重置密码 link。由于某种原因,它没有按预期工作。
它只是继续发送电子邮件,并且不检查电子邮件是否存在,我不知道为什么会这样。
我的代码如下所示:
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'});
})
}
});
};
但这并没有检查任何东西,它只是继续发送电子邮件。我需要帮助。
尝试使用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 });
}
};
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'});
})
}
});
};
我想做一些事情,比如当用户搜索 mongoDB 数据库并且用户存在时,它应该向用户发送一封电子邮件,其中包含重置密码 link。由于某种原因,它没有按预期工作。 它只是继续发送电子邮件,并且不检查电子邮件是否存在,我不知道为什么会这样。
我的代码如下所示:
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'});
})
}
});
};
但这并没有检查任何东西,它只是继续发送电子邮件。我需要帮助。
尝试使用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 });
}
};
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'});
})
}
});
};