I have a web application built on Node.js and I use Knex as my db tool. It has all worked perfectly for months, but now, one of the columns in my db is not updating correctly. I have the columns 'base','bonus','production_bonus','deductions' and 'commission_earned'. The 'commission_earned' column is calculated on the backend of the application and then the values are sent to the db with knex. It is working perfectly for the initial creation of the record, but when I edit the record, it is not updating the 'commission_earned' column correctly. It updates the other values correctly, and when I console.log() the value to be stored for commission_earned it shows the correct value, it just is not updating it.
I have included screenshots of my code below as well as an example for you to see. I tried copying the column name from postgres to ensure it wasn't misspelled and it still doesn't work. So far, I have tried the following:
- Ensured that all of the column names are being used correctly
- Tried using a separate update call to update the value after updating the other values
- Created a postgres function with a trigger that should update the column value any time a row is added
- Tried using knex.raw to run the query that way
- Tried placing the update method after the where clause in knex
I am stuck at this point and have no idea what to do.
P.S I know this db is not properly normalized, I built it when I was first learning how to build with Node
This is my index.js
When I console.log myData.commission_earned
, it returns the correct value, but it does not update it in the db
app.post("/editScore", async (req,res) => {
var score_id = req.body.scoreID;
const myData = await calculateCommission.getData(req.body);
knex('commissions').update(myData).where('id',score_id)
.then( () => {
res.redirect("/commissionLog")
})
});
Below is the code for the initial log, it is working correctly, and they both calculate the commission_earned with the same function
app.post("/submitCommission", async (req,res) => {
//take the data sent in the request and send it to the commissionCalc file for processing
const myData = await calculateCommission.getData(req.body);
//Insert values into DB
knex("commissions").insert(myData)
.then( () => {
res.redirect("/commissionLog")
})
})
I know it is not an issue with the calculation itself since I am able to console.log() and see that the correct value is being returned. And like I mentioned, it is updating the other values correctly, just not the commission_earned column.