Reputation: 2644
This is my code in Node JS and I was using mysql
router.post('/districts', (req, res) => {
const stateid = req.body.stateid;
db.query("select id, name from lkup_district where stateid = '"+stateid+"' ", (error, rows) => {
if(error) {
console.log(error);
}
if(rows.length>0) {
res.send(rows);
}
});
});
Using stateid I am getting the districts data. But if I send some stateid which was not present in lkup_districts, Postman is giving me loading and nothing happens. What's going wrong?
Should I write
if(rows.length=0) {
}
Upvotes: 1
Views: 338
Reputation: 4650
Look. You send answer on request only if you find something. If errors occurred or your request find nothing you do not send any data back. So, IMHO, you need to slightly modify your code. Something like
router.post('/districts', (req, res) => {
const stateid = req.body.stateid;
db.query(
"select id, name from lkup_district where stateid = ? "
,[stateid], (error, rows) => {
if(error) {
console.log(error);
res.status(500).send("Error proceeding query");
}
if(rows.length>0) {
res.send(rows);
}
else {
res.status(404).send("No data found");
}
}
);
});
Upvotes: 2