Reputation: 103
Please help me out. I'm making a search functionality. Below is My Code and User stories. I'm able to pass only the first User Story but not with the other stories. Can someone please help me out.
User Stories:
As an User if I search with Skills and location I should get the results which matched with the query.
As an User if I search with only Skills still I should get the results which matched with the query.
As an User if I search with only Location still I should get the results which matched with the query.
As an User if I search with invalid skills (J, A, B) or location I shouldn't get the results which does matched the query..
var search = {
get: function (req, res) {
var locationQuery = req.params.locationQuery,
skillsQuery = req.params.skillsQuery;
Jobs.find({state: { "$regex": locationQuery, "$options": "i" }, Skills: { "$regex": skillsQuery, "$options": "i" }}, function(err, result) {
if (err) {
console.log('Not a Valid Search');
res.status(500).send(err, 'Not a Valid Search');
}else {
res.json(result);
}
});
}
};
Upvotes: 1
Views: 7384
Reputation: 1705
For this kind of optional support, It would be better to take search parameters as query in GET. You can take reference of the following code snippet.
var search = {
get: function (req, res) {
var query = {};
if(req.query.locationQuery){
query.state = { "$regex": req.query.locationQuery, "$options": "i" };
}
if(req.query.skillsQuery){
query.Skills = { "$regex": req.query.skillsQuery, "$options": "i" }
}
Jobs.find(query, function(err, result) {
if (err) {
console.log('Not a Valid Search');
res.status(500).send(err, 'Not a Valid Search');
}else {
res.json(result);
}
});
}
};
Upadte:
Define your route as:
searchRoute.get('/', search.get);
And you can send your query parameters as(through POSTMAN):
localhost:3000/api/search/?skillsQuery="java,javascript"&locationQuery="Mumbai
Upvotes: 2