I've just started creating a gym tracking app using React Native, Typescript, Node, and MySQL to improve my backend (by backend I mean my proficiency in backend development, I'm not creating the app to track my squats in the hopes of improving my backend, although it could do with some work. Also by gym tracking app I mean essentially a replacement for a gym tracking notepad with some added graphs etc.)
I've created a route that posts the user's latest set with all the usual things like weight, reps, etc.
This is the function that posts to the db:
export const addLogEntry = (req: Request, res: Response) => {
fetch('http://localhost:3000/logEntries', {
method: 'POST',
body: JSON.stringify({ id: req.body.id }),
headers: { 'Content-Type': 'application/json' },
})
.then((response: any) => response.json())
.then((data: any) => {
req.body.entry.id = data.length + 1;
req.body.entry.userId = req.body.id;
const {
id,
set,
logId,
weight,
reps,
time,
rest,
exerciseId,
userId,
} = req.body.entry;
console.log(id, set, logId, weight, reps, time, rest, exerciseId, userId);
connection.query(
`INSERT into logEntries(id, set, logId, weight, reps, time, rest, exerciseId, userId) VALUES(${id}, ${set}, ${logId}, ${weight}, ${reps}, ${time}, ${rest}, ${exerciseId}, ${userId})`,
function (error, results, fields) {
if (error) throw error;
res.send('');
}
);
})
.catch((error) => {
throw error;
});
};
Here's the body of the request:
{
"id": 1,
"entry": {
"set": 2,
"logId": 2,
"weight": 82.5,
"reps": 12,
"time": 0,
"rest": 60,
"exerciseId": 1
}
}
Here is a screenshot of the table structure:
And this is the error:
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set, logId, weight, reps, time, rest, exerciseId, userId) VALUES(9, 2, 2, 82.5, ' at line 1
Does anyone have any idea? I feel like the fact that it's a syntax error means it's most likely staring me in the face.