In my MySQL database, I have columns named infected
and absence
which are stored as TINYINT(1)
.
When I execute the following code in my backend:
app.get("/users", (req, res) => {
const sql = `SELECT * from users`;
connection.query(sql, (err, results) => {
if (err) {
return res.send(err);
} else {
return res.json({ results });
}
});
});
and in my ReactJS frontend:
useEffect(() => {
axios
.get(`${config.server}/users`)
.then((result) => {
setUsers(result.data.results);
})
.catch((error) => console.error(error));
}, [setUsers]);
I encounter an issue where filtering with
users.filter((u) => u.infected);
does not work. Only
users.filter((u) => u.infected === 1);
seems to be effective.
Since filtering using
users.filter((u) => u.infected);
is more intuitive for me, I am wondering how to address this best practice-wise. Should I consider:
- Storing values in a different type?
- Modifying the SELECT query?
- Translating values from 1 to TRUE and 0 to FALSE after querying?
- Adjusting values in the frontend post get request?
I am hesitant about option 4 as my frontend primarily expects booleans now that I have transitioned to MySQL.