首页 > 解决方案 > 模糊搜索中的“未定义正则表达式”错误

问题描述

正则表达式未定义

做了一些搜索,它说使用正则表达式而不是正则表达式,但它给了我一个数据库问题。我应该怎么做才能在我的网站上进行模糊搜索?

router.get("/", function(req, res){
    if(req.query.search){
       const regex = new RegExp(escapeRegex(req.query.search), 'gi');
    } else{
        // Get all campgrounds from DB
        Campground.find({name:regex}, function(err, allCampgrounds){
           if(err){
               console.log(err);
           } else {
              res.render("campgrounds/index",{campgrounds: allCampgrounds, page: 'campgrounds'});
           }

        });
    }
});

标签: node.js

解决方案


推荐阅读