Background
This is the client side Javascript, where I make a post request to update the respective tables with the form parameters. My database has two tables-Rabbit table, and MyStuff table, and MyStuff table holds a foreign key to the rabbit table. Now, I first update the Rabbit table by making a post request which not only updates the Rabbit table but also updates the value of the rabbitID variable in the server, and the second request updates the MyStuff with the respective form parameters, and also makes use of the rabbitID variable just updated.
async function updateAllHelper(formDataParam) {
let updateR = await updateRabbitTable(formDataParam);
let updateMyS = await updateMyStable(formDataParam);
}
async function updateRabbitTable(formDataParam) {
let headers = new Headers();
const response = await fetch("/updateRtable", {
method: "POST",
body: formDataParam,
headers
});
const body = await response.json();
return body;
}
async function updateMyStable(formDataParam) {
let headers = new Headers();
const response = await fetch("/updateMyStable", {
method: "POST",
body: formDataParam,
headers
});
const body = await response.json();
return body;
}
Down below are my endpoints for the server side. In the "updateRtable" endpoint, I update the rabbit table and fetch the maximum rabbit ID and store that value in the lastInsertedRId variable. After that the "updateMyStable" endpoint updates the MyStuff table with the form parameters, and also makes use the lastInsertedRId.
app.post("/updateRtable", (req, res) => {
console.log("went inside rabbit");
console.log(req.body.RName);
let successful = true;
con.query(`INSERT INTO RABBIT(R_ID, RABBIT_NAME, DATE_OF_BIRTH, RABBIT_STATUS, SEX, BREED) VALUES (NULL, '${req.body.RName}', '${req.body.dob}', '${req.body.rStat}', '${req.body.sex}', '${req.body.breed}')`, function(err, result, fields) {
if (err) {
successful = false;
console.log(err);
} else {
//to get rabbitID
console.log("went inside fetching maximum id");
con.query(`SELECT max(R_ID) FROM RABBIT`, function(errinside, r, f) {
if (errinside) {
successful = false;
console.log(errinside);
} else {
lastInsertedRId = JSON.parse(JSON.stringify(r))[0]['max(R_ID)'];
}
});
}
});
if (successful === true) {
res.status(200).send({
success: true
});
} else {
res.status(ERROR_STATUS_CODE).send({
success: false
});
}
});
app.post("/updateMyStable", (req, res) => {
console.log("went inside mystuff table");
let successful = true;
con.query(`INSERT INTO MYSTUFF(R_ID, NOTES, Kibble, Carrier, Medication, Treats, Blankets, Toys, Drop_off_Date, Pick_up_Date) VALUES ('${lastInsertedRId}', '${req.body.notes}', '${req.body.kibble}', '${req.body.carrier}', '${req.body.medic}', '${req.body.treats}', '${req.body.blankets}', '${req.body.toys}', '${req.body.dropD}', '${req.body.picKD}')`, function(err, result, fields) {
if (err) {
console.log(err);
successful = false;
}
});
if (successful === true) {
res.status(200).send({
success: true
});
} else {
res.status(ERROR_STATUS_CODE).send("Server side error");
}
});
Goal
Since the MyStuff table holds a foreign key to the Rabbit table, I obviously want to complete the call to the first endpoint completely(updating the table and fetching the latest ID), and to accomplish that goal, I am trying to make use of async/await where I wait for a response from the server side before executing the next endpoint.
Problem
However, the second endpoint is being executed before the completion of the first endpoint. Specifically, I found out that lastInsertedRId is yet to be updated while the "updateMyStable" endpoint is executed.