错误:将标头发送到客户端后(带有下一个),无法设置标头

时间:2019-01-27 11:46:50

标签: node.js promise sequelize.js next

我正在尝试进行一些数据库查询并检查一些数据。如果某些条件不匹配,则应该使用next(err)向前创建错误。

问题是,它向mir发送错误作为响应,但尝试继续进行..所以我在我的节点js应用中收到了错误。

Purchase.findAndCount({where: {fk_product: productId, fk_buyer: req.decoded.id}}).then((numPurchases) => {
    // product purchased?
    if (numPurchases.count < 1) {
        const errNotBought = new Error("you did not buy this product");
        errNotBought.status = 403;
        return next(errNotBought); // <--- it should break up here
    }
}).then(() => {
    res.send({status: true, data: 'product'}) // <-- stacktrace point this line
})

错误是:未处理的拒绝错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头

1 个答案:

答案 0 :(得分:1)

Purchase.findAndCount({where: {fk_product: productId, fk_buyer: req.decoded.id}}).then(numPurchases => { // product purchased? if (numPurchases.count < 1) { const errNotBought = new Error("you did not buy this product"); errNotBought.status = 403; next(errNotBought); // <--- it should break up here } else { res.send({status: true, data: 'product'}); } }); 仅从当前的回调函数返回,它不会以任何方式停止承诺链。您正在寻找

cmp