我在这里得到了一些很大的帮助来修复我的 GET 请求总是默认为 /(?:)/i。我现在可以将我的 GET 设置为未定义。
但是,这仅在我的数据库中搜索一个字段“BusinessName”时才有效。我的问题是,我如何搜索多个字段,所以“BusinessName”和“Address”。这是代码,当它只搜索一个字段时工作:
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.");
}
});
});
这是我通过使用字段“AddressLine3”和“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.");
}
});
});