Node.js ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置头错误解决
我正在从控制器调用此函数。新添加了Node.js ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置头错误解决,node.js,express,Node.js,Express,我正在从控制器调用此函数。新添加了无轨电车详细信息。需要更新设备作为条件。如果该条件为假,则其工作正常。但如果条件为真,则显示此错误 trollyIOTDeviceReadyToActive: async (req, res, next) => { try { const trolleyDetails = await TrollyInventory.findOne({ where: { trolleyid: req.body.trolleyid } })
无轨电车详细信息。需要更新设备作为条件。如果该条件为假,则其工作正常。但如果条件为真,则显示此错误
trollyIOTDeviceReadyToActive: async (req, res, next) => {
try {
const trolleyDetails = await TrollyInventory.findOne({ where: { trolleyid: req.body.trolleyid } })
if (trolleyDetails.need_device_update) {
console.log("IOT device Calling")
let { customerid, trolleyid, macid, businessid, firstname, lastname } = req.body
axios.post('http://esl.minew.com:9191/V1/Login', {
username: trollyUsername,
password: trollyPassword
}).then(async val => {
let sdata = {
"information": {
"id": "10001",
"barcode": "",
"qrcode": JSON.stringify({ "customerid": customerid, "trolleyid:": trolleyid, "businessid": businessid }),
"label1": "",
"label2": JSON.stringify({ "customerid": customerid, "trolleyid:": trolleyid, "businessid": businessid }),
"label3": "",
"label4": "Customerid: " + customerid, //customerid
"label5": firstname + " " + lastname, //customername
"label6": "",
"label7": "",
"label8": "ACTIVE", //status
"label9": "",
"label10": "",
"label11": "",
"label12": ""
},
"mac": macid,
"storeUuid": "150",
"demoId": "81a36aa1c85f41178cefb0df0be4ba97"
}
await axios({
method: "post",
url: "http://esl.minew.com:9191/V2/pub/binding/update",
data: sdata,
headers: {
"Authorization": "Bearer " + val.data.body.token,
"Content-Type": "application/json"
},
}).then(async d => {
let status = "body" in d.data ? d.data['body']['status'] : 0
if (status != 200) {
return res.status(400).send(BAD_REQUEST(req.i18n.__('invalidtrollymacaddress'), null, req));
}
await TrollyInventory.update({ qurcode: JSON.stringify({ "customerid": customerid, "trolleyid:": trolleyid, "businessid": businessid }) }, { where: { trolleyid: trolleyid } })
next()
}).catch(e => {
console.log(e)
return res.status(500).send(INTERNAL_SERVER_ERROR(null, req))
})
}).catch(e => {
console.log(e)
return res.status(500).send(INTERNAL_SERVER_ERROR(null, req))
})
}
else {
console.log("IOT device Not Calling")
next()
}
} catch (e) {
console.log(e)
return res.status(500).send(INTERNAL_SERVER_ERROR(null, req))
}
},
此处显示错误:
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 (/media/rabbani/_dde_data2/dpay_service/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/media/rabbani/_dde_data2/dpay_service/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/media/rabbani/_dde_data2/dpay_service/node_modules/express/lib/response.js:267:15)
at ServerResponse.send (/media/rabbani/_dde_data2/dpay_service/node_modules/express/lib/response.js:158:21)
at _callee3$ (/media/rabbani/_dde_data2/dpay_service/controller/trollyiotController.js:158:52)
at tryCatch (/media/rabbani/_dde_data2/dpay_service/node_modules/regenerator-runtime/runtime.js:63:40)
at Generator.invoke [as _invoke] (/media/rabbani/_dde_data2/dpay_service/node_modules/regenerator-runtime/runtime.js:293:22)
at Generator.next (/media/rabbani/_dde_data2/dpay_service/node_modules/regenerator-runtime/runtime.js:118:21)
at asyncGeneratorSte
很简单,您已经发送了响应,代码中有一个地方再次发送响应。请您查看代码并告诉e哪里出错了!next()
中间件在做什么?我需要知道如果你的积木起作用是什么水平?它从post方法中得到200吗?`更新`此查询是否更新数据库?您应该添加节点调试并一次移动一个步骤。另外,如果上述方法有效,您很有可能会在下一个中间件中发送响应。。谢谢