【发布时间】:2020-10-12 08:09:23
【问题描述】:
我正在尝试使用以下与被盗CarDb 对象一起使用的数据库函数从 express 中返回一个对象(汽车)的数组,但数据库函数的功能非常好
执行以下操作会返回 404,错误为 UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
router.post("/reportstolen", function (request: express.Request, response: express.Response, next: (err?: Error) => void) {
stolenCarDb.put_car(request.body)
.then(() => {
stolenCarDb.get_available_cops()
.then(cops => {
cops.forEach(cop => {
stolenCarDb.assign_cop_car(cop._id, request.body._id)
.then(() => response.status(201).send(`assgined cop ${cop._id} to car`))
})
})
.then(() => response.status(201).send("created"))
})
.catch(() => response.status(500).send("error"))
.finally(next)
})
【问题讨论】:
标签: node.js typescript loops express promise