My current query looks like this:
router.get('/search?', function(req, res, next) {
var title = req.query.title;
var active = req.query.active;
var sql = "SELECT * from users WHERE title = ? AND active = ?";
connection.query(sql, [title, active], function (error, results, fields) {
res.json({"status": 200, "error": null, "response": results});
});
});
It works it I enter:
localhost:4001/api/v1/users/search?title=mr&active=1
But it doesn't work if one is not set:
localhost:4001/api/v1/users/search?title=mr
{"status":200,"error":null,"response":[]}
How can I make the query and return the desired string regardless if one or the other search parameter is set or not? So like:
localhost:4001/api/v1/users/search?title=mr
localhost:4001/api/v1/users/search?active=1
localhost:4001/api/v1/users/search?title=mr&active=1
localhost:4001/api/v1/users/search?active=1&title=mr