我正在測試一些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