本文介绍了GET请求始终默认为/(?:)/i-如何使其“未定义"? -关于这个主题的第二个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我得到了一些很大的帮助来修复我的GET请求,该请求始终默认为/(?:)/i.我现在可以将GET设置为undefined.

I got some great help on here to fix my GET request always defaulting to /(?:)/i. I can now set my GET to undefined.

但是,这仅在我的数据库中搜索一个字段"BusinessName"时有效.我的问题是,我如何搜索多个字段,所以要搜索"BusinessName"和"Address".这是它的代码,仅在一个字段中搜索即可工作:

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.");
    }
  });
});

这是我尝试通过使用"AddressLine3"和"AddressLine4"字段使其正常工作的尝试:

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感谢您的帮助.我实际上发现我在做错什么,我的数据库中不存在AddressLine4,因此在mongo请求中包含它会导致错误-我确实有AddressLine4,但现在我没有.现在,使用OR运算符,一切都很好,欢呼

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

这篇关于GET请求始终默认为/(?:)/i-如何使其“未定义"? -关于这个主题的第二个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 22:07