I am working to improve the error handling in my node/express app, but am slightly lost.
For example here is a route I have that I want to improve:
app.get("/api/max-value", function (req, res) {
const shop_domain = req.query.shop;
if (shop) {
table = 'max_value_table';
condition = "shop='" + shop + "'";
store.getRow(table, condition, function (data) {
return res.json({
currencySymbol: data[0].currencySymbol,
currencyDecimal: data[0].currencyDecimal,
currencyAmount: data[0].currencyAmount,
})
});
}
});
store.getRow is calling this function, but the error handling throws the error and crashes my app.
getRow: function (table, condition, cb) {
var queryString = "SELECT * FROM " + table + " WHERE " + condition + ";";
console.log('mySQL getRow: ', queryString);
connection.query(queryString, function (err, result) {
if (err) {
throw err;
}
cb(result);
});
},
What I want to add is error handling for the store.getRow function. If the function runs and the condition is not met in the MySQL db return error message / send like a res.send().
Should I amend the if(err) to do something else?
store.getRowreturns a Promise?