I got some great help on here to fix my GET request always defaulting to /(?:)/i. I can now set my GET to undefined.
However, this only works when searching for one field 'BusinessName' in my DB. My question is, how do i search multiple fields, so 'BusinessName' and also 'Address'. Here is the code as it is, working when it searches just one field:
app.get("/widget", function(req, res) {
// This returns just one restaurant and doesnt do partial string matching
/* let test = req.query.search */
// This returns up to 200 results that partially match the search term
/* let test = new RegExp (req.query.search, 'i'); */
// This sets the query to undefined, to start with
const test = (req.query.search)
? new RegExp(req.query.search, 'i')
: undefined
Restaurant.find({
BusinessName: test
}, null, {
limit: 200
}, function(err, foundRestaurant) {
console.log(test);
if (foundRestaurant) {
/* res.send(foundRestaurant) */
res.render("widget", {
foundRestaurant
})
} else {
res.render("widget", "No restaurants matching that title was found.");
}
});
});
Here is my broken attempt to get it to work by using fields 'AddressLine3' and 'AddressLine4':
app.get("/town_widget", function(req, res) {
const test3 = (req.query.search)
? new RegExp(req.query.search, 'i')
: undefined
const test4 = (req.query.search)
? new RegExp(req.query.search, 'i')
: undefined
Restaurant.find({
$or:[{
AddressLine3: test3
},
{
AddressLine4: test4
}]},
null, {
limit: 2
},
function(err, foundTown) {
if (foundTown) {
console.log(foundTown);
res.render("town_widget", {
foundTown
})
} else {
res.render("town_widget", "No town or city matching that input was found/no restaurants found in the town specified.");
}
});
});
O.Jones thanks for your help. I actually found what I was doing wrong, AddressLine4 doesn't exist in my DB and so including it in my mongo request resulted in the error - I did have AddressLine4 at one point but now I don't. All works great now with the OR operator, cheers