gpt4 book ai didi

javascript - 错误 [ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client, 我无法发送表单

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:37 25 4
gpt4 key购买 nike

每当我想在我的 View (ejs)中填写表单并发送到数据库时,我总是在终端中收到此错误。

    MongoDB Connected



Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:485:11)
at ServerResponse.header (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/response.js:170:12)
at done (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/response.js:1004:10)
at tryHandleCache (/home/codabae/Documents/mailmonster/api/v1/node_modules/ejs/lib/ejs.js:257:5)
at View.exports.renderFile [as engine] (/home/codabae/Documents/mailmonster/api/v1/node_modules/ejs/lib/ejs.js:482:10)
at View.render (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/view.js:135:8)
at tryRender (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/application.js:640:10)
at Function.render (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/response.js:1008:7)
at /home/codabae/Documents/mailmonster/api/v1/node_modules/express-ejs-layouts/lib/express-layouts.js:113:20
at tryHandleCache (/home/codabae/Documents/mailmonster/api/v1/node_modules/ejs/lib/ejs.js:257:5)
at View.exports.renderFile [as engine] (/home/codabae/Documents/mailmonster/api/v1/node_modules/ejs/lib/ejs.js:482:10)
at View.render (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/view.js:135:8)
at tryRender (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/application.js:640:10)
at Function.render (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/application.js:592:3)

这是我的代码

我的看法


<h1 class="mt-4">Dashboard</h1>
<div class="row mt-5">
<div class="col-md-6 m-auto">
<div class="card card-body">
<h1 class="text-center mb-3">
<i class="fas fa-user-plus"></i> Add Smtp
</h1>
<% include ./partials/messages %>
<form action="/users/mail" method="POST">
<div class="form-group">
<label for="smtpUsername">smtpUsername</label>
<input
type="name"
id="smtpUsername"
name="smtpUsername"
class="form-control"
placeholder="Enter smtpUsername"
value="<%= typeof smtpUsername != 'undefined' ? smtpUsername : '' %>"
/>
</div>
<div class="form-group">
<label for="smtpPassword">smtpPassword</label>
<input
type="name"
id="smtpPassword"
name="smtpPassword"
class="form-control"
placeholder="Enter smtpPassword"
value="<%= typeof smtpPassword != 'undefined' ? smtpPassword : '' %>"
/>
</div>
<div class="form-group">
<label for="smtpUrl">smtpUrl</label>
<input
type="name"
id="smtpUrl"
name="smtpUrl"
class="form-control"
placeholder="Enter smtpUrl"
value="<%= typeof smtpUrl != 'undefined' ? smtpUrl : '' %>"
/>
</div>
<button type="submit" class="btn btn-primary btn-block">
Add Smtp
</button>
</form>
</div>
</div>
</div>



<div class="row mt-5">
<div class="col-md-6 m-auto">
<div class="card card-body">
<h1 class="text-center mb-3">
<i class="fas fa-user-plus"></i> send mail
</h1>
<% include ./partials/messages %>
<form action="/users/mail" method="POST">
<div class="form-group">
<label for="to">to</label>
<input
type="name"
id="to"
name="to"
class="form-control"
placeholder="Enter to"
value="<%= typeof to != 'undefined' ? to : '' %>"
/>
</div>
<div class="form-group">
<label for="bcc">bcc</label>
<input
type="name"
id="bcc"
name="bcc"
class="form-control"
placeholder="Enter bcc"
value="<%= typeof bcc != 'undefined' ? bcc : '' %>"
/>
</div>
<div class="form-group">
<label for="cc">cc</label>
<input
type="name"
id="cc"
name="name"
class="form-control"
placeholder="Enter cc"
value="<%= typeof cc != 'undefined' ? cc : '' %>"
/>
</div>
<div class="form-group">
<label for="subject">subject</label>
<input
type="name"
id="subject"
name="subject"
class="form-control"
placeholder="Enter subject"
value="<%= typeof subject != 'undefined' ? subject : '' %>"
/>
</div>
<div class="form-group">
<label for="message">message</label>
<input
type="name"
id="message"
name="message"
class="form-control"
placeholder="Enter message"
value="<%= typeof message != 'undefined' ? message : '' %>"
/>
</div>
<button type="submit" class="btn btn-primary btn-block">
Register
</button>
</form>
</div>
</div>
</div>



<a href="/users/logout" class="btn btn-secondary">Logout</a>

我的路线


router.get('/mail', forwardAuthenticated, (req, res) =>
res.render('mail', {
user: req.user,
mail: req.mail

})
);
router.post('/mail', (req, res) => {
const { to, cc, bcc, subject, message, attachment,smtpUrl, smtpUsername, smtpPassword } = req.body;

console.log(smtpUrl)
console.log(smtpPassword)
console.log(smtpUsername)



let errors = [];

if (!smtpUrl || !smtpUsername || !smtpPassword) {
errors.push({ msg: 'Add an account' });
res.render('mail', {
smtpUrl,
smtpPassword,
smtpUsername
});

}else{
console.log(smtpUrl)
console.log(smtpPassword)
console.log(smtpUsername)
const newSmtp = new Smtp({
smtpUrl,
smtpPassword,
smtpUsername
});

newSmtp
.save()
.then(mail => {
req.flash(
'success_msg',
'Account Added'
);

})
.catch(err => console.log(err));
}
if (!to || !subject || !message) {
errors.push({ msg: 'Please enter all fields' });
}


if (errors.length > 0) {
res.render('mail', {
errors,
to,
cc,
bcc,
subject,
message,
attachment,
});
} else {
const newMail = new Mail({
to,
cc,
bcc,
subject,
message,
attachment,
});


let transporter = nodemailer.createTransport({
service: smtpUrl,
auth: {
user: smtpUsername,
pass: smtpPassword
}
});

let mailOptions = {
from: smtpUsername,
to: to,
subject: subject,
text: `${message}`
};

transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {

newMail
.save()
.then(mail => {
req.flash(
'success_msg',
'mail sent'
);

})
.catch(err => console.log(err));
console.log('Email sent: ' + info.response);
}
});





}


})

// Login
router.post('/login', (req, res, next) => {
passport.authenticate('local', {
successRedirect: '/users/mail',
failureRedirect: '/users/login',
failureFlash: true
})(req, res, next);
});

// Logout
router.get('/logout', (req, res) => {
req.logout();
req.flash('success_msg', 'You are logged out');
res.redirect('/users/login');
});

我的架构


const mongoose = require('mongoose');

const MailSchema = new mongoose.Schema({
to: {
type: String,

},
cc: {
type: String,

},
bcc: {
type: String,
},
subject: {
type: String,

},
message: {
type: String,

},
attachment: {
type: String,
},
date: {
type: Date,
default: Date.now
},

});

const SmtpSchema = new mongoose.Schema({
smtpUrl: {
type: String,
required: true

},
smtpUsername: {
type: String,
required: true

},
smtpPassword: {
type: String,
required: true

},
date: {
type: Date,
default: Date.now
},

});





const Mail = mongoose.model('Mail', MailSchema);
const Smtp = mongoose.model('Smtp', SmtpSchema);


module.exports = Mail;
module.exports = Smtp;


每当我想在我的 View (ejs)中填写表单并发送到数据库时,我总是在终端中收到此错误。

错误 [ERR_HTTP_HEADERS_SENT]:将 header 发送到客户端后无法设置 header ,我无法发送表单错误 [ERR_HTTP_HEADERS_SENT]:将 header 发送到客户端后无法设置 header ,我无法发送表单错误 [ERR_HTTP_HEADERS_SENT]:将 header 发送到客户端后无法设置 header ,我无法发送表单错误 [ERR_HTTP_HEADERS_SENT]:将 header 发送到客户端后无法设置 header ,我无法发送表单

最佳答案

当您在router.post('/mail'中添加smtp部分出错时,您会继续发送邮件,因此在某些情况下您应该两次向客户端发送响应,这是不可能的。您的根本问题是您在单个路由器路径和方法中执行两项不同的工作。我强烈建议将两者分开。我看到您有另一个问题,在单个页面中具有相同操作的 2 个表单(它是 POST 在路径 /mail 中),通过分离该问题将得到解决。

关于javascript - 错误 [ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client, 我无法发送表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59919809/

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