未处理的承诺拒绝:在将标头发送到客户端后无法设置标头

时间:2018-05-29 06:30:06

标签: node.js angular api express header

我不知道为什么会出现这种错误。我在互联网上搜索所有可能的解决方案仍然没有找到任何解决方案。这是我的API调用节点函数。

exports.GetEmployeeConfirmationList = function (req, res) {
    var request = dbConn.request();
    request.execute(storedProcedures.GetEmployeeConfirmationList).then(function (response) {
        res.status(200).send({
            success: true,
            data: response.recordset
        });
    }).catch(function (err) {
        res.status(200).send({
            success: false,
            message: err.message
        });
    });
};

如何克服这个问题? 提前谢谢。

1 个答案:

答案 0 :(得分:2)

修改代码以使用箭头功能,因为res对象正在失去其范围。 调用GetEmployeeConfirmationList回调时,它不知道res对象因此undefined。因此,在执行res.status时,它会抛出异常并进入catch块,再次执行res.status并再次中断并导致未处理的承诺拒绝。 您可以在err块中捕获log对象和finally对象,以检查它是否正在发生。

exports.GetEmployeeConfirmationList = function (req, res) {
    var request = dbConn.request();
    request.execute(storedProcedures.GetEmployeeConfirmationList).then((response) => {
        res.status(200).send({
            success: true,
            data: response.recordset
        });
    }).catch((err) => {
        res.status(200).send({
            success: false,
            message: err.message
        });
    });
};