gpt4 book ai didi

node.js - 未处理的PromiseRejection警告: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

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

enter image description here当我尝试在同一篇文章中发送和接收数据时,我会收到此错误。我无法找出问题所在。

但这并不是代码根本不起作用,而是数据显示得非常好。只是我在 bash 控制台中收到此错误。

router.post('/add',(req, res) => {

const newAMCReg = new AMCReg({
amcrefno: req.body.amcrefno,
amcregdate: req.body.amcregdate,
customer: req.body.customerid,
customertype: req.body.customertypeid,
department: req.body.customersubdepartmentid,
serviceprovider: req.body.serviceproviderid,
amcstartdate: req.body.amcstartdate,
amcexpiredate: req.body.amcexpiredate,
remarks: req.body.remarks
});
newAMCReg.save()
.then((amcid) => {

AMCReg.findOne({amcrefno: req.body.amcrefno})
.then(amc => res.json(amc))
.then(amc => {
res.status(200).json({ msg: "AMC Registration Updated Successfully" });
})
.catch(err => res.status(500).json({msg: "Internal Server Error"}));
})
});

最佳答案

您不应多次发送回复,请参阅下文:

router.post('/add', (req, res) => {

const newAMCReg = new AMCReg({
amcrefno: req.body.amcrefno,
amcregdate: req.body.amcregdate,
customer: req.body.customerid,
customertype: req.body.customertypeid,
department: req.body.customersubdepartmentid,
serviceprovider: req.body.serviceproviderid,
amcstartdate: req.body.amcstartdate,
amcexpiredate: req.body.amcexpiredate,
remarks: req.body.remarks
});
newAMCReg.save()
.then((amcid) => {

AMCReg.findOne({
amcrefno: req.body.amcrefno
})
.then(amc => {
res.status(200).json({
msg: "AMC Registration Updated Successfully",
data: amc
});
})
.catch(err => res.status(500).json({
msg: "Internal Server Error"
}));
})
});

关于node.js - 未处理的PromiseRejection警告: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56087700/

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