我不知道为什么这个错误不断出现。我在互联网上搜索了所有可能的解决方案,但仍然没有找到。这是我用于 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
});
});
};
如何克服这个问题?
修改代码以使用箭头函数,因为
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
});
});
};