I have developed a Backend route to retrieve games based on specific letters provided. Below are the two routes that I implemented:
router.get("/public/gamelist/:letter", (req, res, next) => {
var sql = "SELECT title FROM Games WHERE title LIKE ? || '%' AND ownage = 'true'"
var params = [req.params.letter]
db.all(sql, params, (err, rows) => {
if (rows) {
return res.status(200).json(rows);
} else if (!rows) {
return res.json({ "answer": "NoGame" })
} else if (err) {
res.status(400).json({ "error": err.message });
return;
}
});
});
router.get("/public/game/:title", (req, res, next) => {
var sql = "select * from Games where title = ?"
var params = [req.params.title]
db.get(sql, params, (err, row) => {
if (row) {
res.status(200).json(row);
} else if (!row) {
console.log("Dont exist")
return res.json({ "answer": "NoGame" })
} else if (err) {
res.status(400).json({ "error": err.message });
return;
}
});
});
The second route /public/game/:title
is functioning correctly, however, the first one /public/gamelist/:letter
is not handling empty results properly. It consistently returns a 200
status code even when no game matches the query, returning an empty array instead of
({ "answer": "NoGame" })
Can anyone identify what might be incorrect in this portion of my code?