我正在测试一些NodeJS设置API的测试。UnhandledPromiseRejectionWarning:未处理的承诺拒绝NodeJS MSSQL
但是,当SQL返回有关NULL列的错误时,我的http调用会挂起,您可以在节点控制台中看到错误。
我得到的错误是
node:7397) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): RequestError: Cannot insert the value NULL into column 'FeatureEntryId', table 'testdb STING.dbo.J_ProductFeaturesRelation'; column does not allow nulls. INSERT fails."
在了解SQL错误,这很好我只是想给的NodeJS返回此错误信息给客户端。
这是代码。
//Add or update feature relations
var insertFeatureRelation = (callback, productid, featureid) => {
console.log(productid + ' ' + featureid)
var conn = new sql.Connection(settings.dbConfig())
conn.connect().then(function (conn) {
var request = new sql.Request(conn);
request.input('productid', sql.VarChar, productid);
request.input('featureid', sql.VarChar, featureid);
request.execute('PM_InsertFeatureRelation').then(function (recordsets, returnValue, affected) {
callback(recordsets)
})
}).catch(function (err) {
console.log('ffs');
callback(null, err);
});
}
exports.insertFeatureRelation = function (req, resp, productid, featureid) {
insertFeatureRelation(function (data, err) {
if (err) {
httpMsgs.show500(req, resp, err)
} else {
httpMsgs.sendJSON(req, resp, data)
}
resp.end();
}, productid, featureid)
};
如果存储过程运行正常,但代码工作正常,但错误只是实际上一直到页面。
这里是httpMsgs的内容:
exports.show500 = function(req, resp, err) {
console.log('heree')
resp.writeHeader(500, {"Content-Type": "application/json"});
resp.write(JSON.stringify({data: "Error:" + err}))
};
exports.sendJSON = function(req, resp, data) {
if (data){
resp.writeHeader(200, {"Content-Type": "application/json"});
resp.write(JSON.stringify(data));
}
}
你的问题是什么? – MrJLP
@MrJLP为什么httpMsgs.show500(req,resp,err)在SQL返回错误时不会触发。我会认为它会在catch中发现错误,然后运行console.log('ffs')和回调函数。 – Adam91Holt
失败的承诺是'request.execute('PM_InsertFeatureRelation')',你没有抓住它。 – naortor