我尝试在创建新资源后发送邮件,并且在创建新用户之前,它应检查电子邮件和员工编号,如果其中任何一个值应该显示已经存在或者它将添加新资源并且必须发送邮件。
如果email和emp no不相同,则它完美地创建了资源并发送邮件。但是,如果我尝试相同的邮件或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: 'noemail@gmail.com',
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" <noemail@gmail.com>',
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);
});
});
}
});
});
答案 0 :(得分:2)
问题是由你的if else条件中的缺陷引起的。当第一个匹配时
var canvas = new fabric.Canvas('c');
let json = {"objects":[{"type":"rect","originx":"left","originy":"top","left":323,"top":259,"width":50,"height":300,"fill":"#ff5b6d","stroke":null,"strokewidth":0,"strokedasharray":null,"strokelinecap":"butt","strokelinejoin":"miter","strokemiterlimit":10,"scalex":1.54,"scaley":1.54,"angle":0,"flipx":false,"flipy":false,"opacity":1,"shadow":null,"visible":true,"clipto":null,"backgroundcolor":"","fillrule":"nonzero","globalcompositeoperation":"source-over","transformmatrix":null,"skewx":0,"skewy":0,"rx":0,"ry":0},{"type":"rect","originx":"left","originy":"top","left":205,"top":198,"width":50,"height":300,"fill":"#ff5b6d","stroke":null,"strokewidth":1,"strokedasharray":null,"strokelinecap":"butt","strokelinejoin":"miter","strokemiterlimit":10,"scalex":1,"scaley":1,"angle":0,"flipx":false,"flipy":false,"opacity":1,"shadow":null,"visible":true,"clipto":null,"backgroundcolor":"","fillrule":"nonzero","globalcompositeoperation":"source-over","transformmatrix":null,"skewx":0,"skewy":0,"rx":0,"ry":0}]}
canvas.loadFromJSON(json);
else if语句也匹配。所以res.redirect被触发两次。
请优化您的if / else条件以确保没有重叠。