I created a Backend route to fetch games with specific provided letters. Below you can find two routes i created:
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
works as expected, while the first one /public/gamelist/:letter
doesn´t catch the empty result. It always returns 200
, when no game exists it simply returns an empty array instead of ({ "answer": "NoGame" })
Does anyone have an idea what's wrong with this part of my code?
CodePudding user response:
// Happy coding guys
const express=require('express')
const routes = require('./routes')
const app=express()
app.set("view engine","ejs")
app.use(express.urlencoded({ extended: true }))
app.use("/", routes)
app.listen(3000,()=>{
console.log("SERVER ACTIVE");
})
CodePudding user response:
static addSongGet(request,response){
let error=request.query.error
if (!error) {
error=[]
}else{
error=error.split(',')
}
console.log(error);
ModelSongs.addSongGet((err,data)=>{
if (err) {
response.send(err)
}else{
// console.log(error);
response.render("songsAdd",{data,error})
}
})
}
static addSongPost(request,response){
// console.log('ini dari control');
ModelSongs.addSongPost(request,(err,data)=>{
if (err) {
// console.log(err);
response.redirect(`/songs/add?error=${err}`)
}else{
response.redirect('/songs')
}
})
}