Currently, I am developing an inline grid editor that triggers a call to an express rest api whenever a user updates a single value in the grid. The issue I am facing is that when a field is changed, my PATCH
request attempts to update all fields instead of just the one that was modified. This causes any fields with no value to be set as NULL in the database. What I aim to achieve is to only update the specific field that is passed into the API, which could be any field. Below is the method I am using for patching:
// Update record based on TxnID
router.patch('/editablerecords/update', function (req, res) {
let qb_TxnID = req.body.txnid
let type = req.body.type;
let margin = req.body.margin;
if (!qb_TxnID) {
return res.status(400).send({ error:true, message: 'Please provide TxnID' });
}
connection.query("UPDATE pxeQuoteToClose SET ? WHERE qb_TxnID = '" + qb_TxnID + "'", { type, margin }, function (error, results, fields) {
if(error){
res.send(JSON.stringify({"status": 500, "error": error, "response": null }));
} else {
res.send(JSON.stringify({ error: false, data: results, message: 'Record updated.' }));
}
});
});
I intend to update only one field at a time, either type
or margin
, without affecting the other (if not specified). If only one field is sent, the unmentioned field will default to null. I have researched the connection.query()
method, but details about how it constructs the query are unclear. It seems every req.body.value
influences the query.
This is my first attempt at building a REST API and I suspect there might be a simple solution eluding me.
EDIT: It's worth mentioning that while I may eventually need to update both fields, currently I prefer updating them individually. Thank you.