Javascript 错误:Can';t在发送邮件后设置邮件头。过帐数据
我正在使用此API发布公寓数据,数据正在注册,但我总是收到错误消息:“发送后无法设置头。” 我如何解决此实现以避免这种情况? 我用所有堆栈来补充代码,以访问此APIJavascript 错误:Can';t在发送邮件后设置邮件头。过帐数据,javascript,node.js,express,Javascript,Node.js,Express,我正在使用此API发布公寓数据,数据正在注册,但我总是收到错误消息:“发送后无法设置头。” 我如何解决此实现以避免这种情况? 我用所有堆栈来补充代码,以访问此API app.use(cors()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.get('/apartment', (req, res) => { Apartment.findAll({ at
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/apartment', (req, res) => {
Apartment.findAll({
attributes: [
'apartment_id',
'apartment_floor',
'apartment_size',
'apartment_block_id',
'apartment_block_name',
'apartment_parking_id',
],
})
.then((apartments) => {
// console.log(documents);
res.sendStatus(200).send(apartments);
})
.catch((err) => console.log('erro: ', err));
});
app.get('/apartment/:apartment', (req, res) => {
Apartment.findByPk(
{
attributes: [
'apartment_id',
'apartment_block_id',
'apartment_floor',
'apartment_size',
'apartment_block_name',
'apartment_parking_id',
],
where: {
apartment_id: req.params.apartment_id,
apartment_block_id: req.params.apartment_block_id,
},
},
)
.then((documents) => {
res.sendStatus(200).send(documents);
})
.catch((err) => console.log("erro - API GET '/document: ", err));
});
app.post('/apartment', (req, res) => {
if (!req.body) {
return res.sendStatus(400).send({
message: 'Content can not be empty!',
});
} else {
Apartment.create({
apartment_id: req.body.apartment_id,
apartment_block_id: req.body.apartment_block_id,
apartment_block_name: req.body.apartment_block_name,
apartment_floor: req.body.apartment_floor,
apartment_size: req.body.apartment_size,
apartment_parking_id: req.body.apartment_parking_id,
});
}
return res
.sendStatus(200)
.send({ message: ' [CREATED REGISTER] API POST: /apartment' });
});
app.listen(config.port, () =>
console.log(`Server listening on port ${config.port}!`),
);
尝试添加
return
在。。。。
即
尝试添加
return
在。。。。
即
您的代码:
res.sendStatus(200).send(apartments);
相当于:
res.status(200).send('OK').send(apartments)
这意味着您尝试发送两次响应:
res
.status(200) // Set status code of response
.send('OK') // Send the response!
.send(apartments) // Error: Can't set headers after they are sent
尝试设置状态
并使用发送
:
res
.status(200) // Set status code of response
.send(apartments) // Send the response
另外,在所有res.status().send()
中添加return
,以避免每次请求多次尝试。您的代码:
res.sendStatus(200).send(apartments);
相当于:
res.status(200).send('OK').send(apartments)
这意味着您尝试发送两次响应:
res
.status(200) // Set status code of response
.send('OK') // Send the response!
.send(apartments) // Error: Can't set headers after they are sent
尝试设置状态
并使用发送
:
res
.status(200) // Set status code of response
.send(apartments) // Send the response
另外,在所有
res.status().send()
中添加return
,以避免每次请求都进行多次尝试。显示您的root
文件?显示您的root
文件?我添加了return,但仍然显示相同的错误try:return res.status(400).json({消息:“内容不能为空!”,})@RudsonRodrigues-如果添加此返回并没有停止错误,那么错误显然来自于除您在这里显示的代码之外的其他代码,因为添加此返回将停止此代码中的特定错误。我添加了返回,但仍然呈现相同的错误尝试:return res.status(400).json({消息:'内容不能为空!',})@RudsonRodrigues-如果添加此返回并不能停止错误,那么错误显然来自于除您在此处显示的代码之外的其他代码,因为添加此返回将停止此代码中的特定错误。谢谢@Rshomon,但我在更新代码后面临同样的问题。错误:发送后无法设置标题。在ServerResponse.send(..\parking\u backend\node\modules\express\lib\response.js:771:10)的ServerResponse.setHeader(..\parking\u Outline.js:498:3)的validateHeader(..\parking\u Outline\node\modules\express\lib\response.js:771:10)上在ServerResponse.json…谢谢@Rshomon,但在更新代码后我面临同样的问题。错误:发送后无法设置头。在validateHeader(_http_outing.js:491:11)在ServerResponse.setHeader(_http_outing.js:498:3)在ServerResponse.header(..\parking_backend\node\u modules\express\lib\response.js:771:10)在validateHeader在ServerResponse.json上发送(..\parking\u backend\node\u modules\express\lib\response.js:170:12)。。。