I'm having a play about testing out some NodeJS setting up an API.
However when SQL returns an error about NULL columns my http call just hangs and you can see the error in the node console.
The error I get is "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."
Here is the code.
//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)
};
The code works fine if the stored procedure runs fine however the error just never actually somes through to the page.
Here is the content of the 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));
}
}
Thanks for looking have a great day!
via Adam91Holt
No comments:
Post a Comment