So I have 1 request that is
app.get('/assignment/loans', (req, res) => {
const id = req.query.bookID;
}
and another that is this:
app.get('/assignment/loans', (req, res) => {
const id = req.query.studentID;
}
For some reason, the studentID one does not work and it always goes to the bookID to search. I use this
http://localhost:3000/assignment/loans?bookID=1
and it works as intended but if I use
http://localhost:3000/assignment/loans?studentID=9653
I get:
which is an error that should only appear for the bookID. How can I differentiate the two? Thank you in advance.
More code regarding the two requests: https://pastebin.com/1A0jK3BX
CodePudding user response:
You cannot have 2 routes with same url. first one take precedence. You need to handle it like this.
app.get('/assignment/loans', (req, res) => {
const bookID = req.query.bookID;
const studentID = req.query.studentID;
if(bookID){
}
if(studentID){
}
}
CodePudding user response:
actually express framework will execute the first endpoint when request happened which is
app.get('/assignment/loans', (req, res) => { const id = req.query.bookID;})
in duplicated endpoint case, so you should using one route and put tow queries instead of one so the code will be like this
app.get('/assignment/loans', (req, res) => {
const book_id = req.query.bookID;
const student_id = req.query.studentID
})
also you have to make validation for queries before executed in db
CodePudding user response:
You have exact same routes
app.get('/assignment/loans', ...)
it has to be one route.
Try combining em like this below
app.get('/assignment/loans', (req, res) => {
const bookId = req.query.bookID;
const studentId = req.query.studentID;
const loans = [];
if(bookId){
if(!Number.isInteger(parseInt(studentId))){
return res.status(422)
.setHeader('content-type', 'application/json')
.send({error: 'bookId not numeric'});
}
db.all('SELECT * FROM loan WHERE bookID=?', bookId, (err, rows) => {
if (err) {
res.status(422)
.setHeader('content-type', 'application/json')
.send({error: 'Problem while querying database'});
return;
}
if (rows.length === 0) {
res.status(404)
.setHeader('content-type', 'application/json')
.send({error: 'loan bookId was not found!'});
} else {
rows.forEach(row =>
loans.push({id: `${row.id}`, bookID: `${row.bookID}`}));
res.status(200)
.setHeader('content-type', 'application/json')
.send(loans);
}
});
}else if(studentId){
if(!Number.isInteger(parseInt(studentId))){
return res.status(422)
.setHeader('content-type', 'application/json')
.send({error: 'studentId not numeric'});
}
db.all('SELECT * FROM loan WHERE studentID=?', studentId, (err, rows) => {
if (err) {
return res.status(422)
.setHeader('content-type', 'application/json')
.send({error: 'Problem while querying database'});
}
if (rows.length === 0) {
res.status(404)
.setHeader('content-type', 'application/json')
.send({error: 'loan studentId was not found!'});
} else {
rows.forEach(row =>
loans.push({id: `${row.id}`, studentID: `${row.studentID}`}));
res.status(200)
.setHeader('content-type', 'application/json')
.send(loans);
}
});
}else{
return res.status(400)
.setHeader('content-type', 'application/json')
.send({error: 'Please provide required query string'});
}
});
CodePudding user response:
Simple answer:
app.get('/assignment/loans', (req, res) => {
const id = req.query.studentID ? req.query.studentID : req.query.bookID
}