我正在尝试使用搜索表单搜索mongo数据库,以将结果显示到我的露营地/搜索路线。我已经成功添加了一个模糊搜索,可以搜索数据库的多个部分,但是,当我尝试使用下拉列表指定类别搜索时,它将破坏代码。
我尝试添加
工作功能
router.get("/search", function(req, res){
var text = req.query.searchAll;
const category = req.query.searchcategory;
if(req.query.searchAll) {
const regex = new RegExp(escapeRegex(req.query.searchAll), 'gi');
Campground.find({ $or: [ {title: regex}, {body: regex}, {subject: regex} ] }, function(err, allCampgrounds){
if(err){
console.log(err);
} else {
res.render("campgrounds/search", {campgrounds:allCampgrounds, text: text});
}
});
} else {
Campground.find({}, function(err, allCampgrounds){
if(err){
console.log(err);
} else {
res.render("campgrounds/search", {campgrounds:allCampgrounds, text:text});
}
});
}
新的损坏功能
router.get("/search", function(req, res){
var text = req.query.searchAll;
const category = req.query.searchcategory;
if(req.query.searchAll) {
const regex = new RegExp(escapeRegex(req.query.searchAll), 'gi');
Campground.find({ $or: [ {title: regex}, {body: regex}, {subject: regex} ] }, function(err, allCampgrounds){
if(err){
console.log(err);
} else {
res.render("campgrounds/search", {campgrounds:allCampgrounds, text: text});
}
});
} else if(req.query.searchcategory) {
Campground.find({category:category}, function(err, allCampgrounds){
if(err){
console.log(err);
} else {
res.render("campgrounds/search", {campgrounds:allCampgrounds, text:text});
}
});
} else {
Campground.find({}, function(err, allCampgrounds){
if(err){
console.log(err);
} else {
res.render("campgrounds/search", {campgrounds:allCampgrounds, text:text});
}
});
}
当我尝试用第二次加载的代码加载我的应用程序时,出现错误
module.exports =路由器;语法错误