0
我嘗試在創建新資源後嘗試發送郵件,並且在創建新用戶之前,應檢查電子郵件和員工編號,如果其中任何一個值相同顯示已存在,否則將添加新資源併發送郵件。節點郵件錯誤:發送後無法設置標題
如果email和emp不一樣,它完全創建資源併發送郵件。但是,如果我試圖同一個郵件或EMP數量正在逐漸錯誤
我的錯誤:
events.js:182
throw er; // Unhandled 'error' event
^
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:489:11)
at ServerResponse.setHeader (_http_outgoing.js:496:3)
at ServerResponse.header (D:\hourmint\node_modules\express\lib\response.js:7
30:10)
at ServerResponse.location (D:\hourmint\node_modules\express\lib\response.js
:847:15)
at ServerResponse.redirect (D:\hourmint\node_modules\express\lib\response.js
:885:18)
at D:\hourmint\app\routes.js:298:18
at D:\hourmint\node_modules\mongoose\lib\query.js:2917:18
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
我變送器:
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: 'mypassword'
},
});
我的代碼:
app.post('/addresources', isLoggedIn, function(req, res) {
var id = (req.body._id) ? req.body._id : mongoose.Types.ObjectId(0);
var newPwd = new mongo.resource();
var pwd = newPwd.generateHash(req.body.res_password);
var resourceValue = {
'role_id': req.body.role_id,
'privilege_id': req.body.privilege_id,
'res_fname': req.body.res_fname,
'res_lname': req.body.res_lname,
'res_email': req.body.res_email,
'res_password': pwd,
'res_empno': req.body.res_empno,
'res_city': req.body.res_city,
'res_mobile': req.body.res_mobile,
'res_emerno': req.body.res_emerno,
'res_hourlyrate': req.body.res_hourlyrate,
'res_otp': 0,
'res_status': req.body.res_status
};
mongo.resource.findOne({
$or: [{
'res_email': req.body.res_email
}, {
'res_empno': req.body.res_empno
}]
}).exec(function(err, user) {
if (err) throw err;
if (user) {
console.log(user)
if (user.res_email == req.body.res_email && user.res_empno == req.body.res_empno) {
req.flash('error', 'Email And Employee Number Already Exists!!!')
res.redirect('/resources')
} //user already exists with email AND/OR phone.
else if (user.res_email == req.body.res_email) {
req.flash('error', 'Email Id Already Exists!!!')
res.redirect('/resources')
} //no users with that email NOR phone exist.
else(user.res_empno == req.body.res_empno) {
req.flash('error', 'Employee Number Already Exists!!!')
res.redirect('/resources')
}
} else {
mongo.resource.findOneAndUpdate({
'_id': id
}, resourceValue, {
upsert: true,
new: true
}, function(err, response) {
if (err) throw err;
console.log("Resource Added SuccessFully")
console.log(response.res_email)
res.redirect('/resources')
var mainOptions = {
from: '"Niyati" <[email protected]>',
to: response.res_email,
subject: 'Hello ✔', // Subject line
text: 'Hello world ?', // plaintext body
html: '<b>Hello world ?</b>' // html body
};
//console.log("html data ======================>", mainOptions.html);
smtpTransport.sendMail(mainOptions, function(err, info) {
if (err) throw err;
console.log("mail Sent");
// console.log(err);
});
});
}
});
});
感謝回覆...我已經使用該條件來顯示Flash郵件作爲電子郵件和員工編號已經存在,如果更改它意味着我可以如何顯示 – Vishnu
add && user.res_empno!= req.body。 res_empno到第二個條件;添加&& user.res_email!= req.body.res_email到最後一個條件 – spiritwalker
謝謝我修正了錯誤,並且在If(User)的最後一個條件中,我改爲else if – Vishnu