I have some columns (infected
and absence
) in my MySQL database that are stored as TINYINT(1)
.
When I do a simple
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 });
}
});
});
in my backend and a
useEffect(() => {
axios
.get(`${config.server}/users`)
.then((result) => {
setUsers(result.data.results);
})
.catch((error) => console.error(error));
}, [setUsers]);
in my ReactJS frontend, I am not able to do something like:
users.filter((u) => u.infected);
Only
users.filter((u) => u.infected === 1);
is working.
As using users.filter((u) => u.infected);
is more intuitive for me, I want to know, how can I handle this in best practice?
I try to avoid using 4. unless this is not the best deal, because my frontend already expects booleans all over the place (I switched the database to MySQL).
Try using !!
they will convert the value to a boolean type.
It's equivalent with: Boolean(anyValue)
.
users.filter(u => !!u.infected);