使用不同的nodejs参数搜索

问题描述 投票:0回答:1

我有一个应用程序,我需要搜索一些参数,必需参数,而其他则不需要。参数不是必需的。

我需要对这两个参数是否不是强制性的要求进行冷漠的搜索。或只有两个强制性之一。

var find = Teacher.find({
      'user': {
        "$in": onlines_clean
      },
      'teacher_del': true,
      'language': language,
      skill: {
        $in: [category]
      },
      'hour.hour_time': hora,
      $and: [{
        "hour.pricea": {
          $lte: price
        }
      }, {
        "hour.priceb": {
          $gte: price
        }
      }]
    })
  }

到目前为止,太好了当我想添加两个不是必须的参数时,问题就来了

      expression: {
        $in: [speak]
      },
      'native': native

     var native= native;
      if (req.params.native) {
     native= req.params.native;
  }

with

 conditions if - else 

这两种方式都不适合我,因为这条路线迫使我必须遵守此规则

/add/exit/:language/:category/:hora/:price/:native?/:speak?

我必须传递原始参数,然后说,我不能忽略本地参数

与req.query相同的事物

我该如何解决?

javascript node.js mongodb express
1个回答
0
投票

[如果可能,我将更改路由以改为使用GET查询参数,因为这些参数没有顺序,可以被忽略而不会引起此问题。

如果无法更改路径的外观,建议使用一个特殊值,该值不代表任何值,例如“ null”:

/add/exit/:language/:category/:hora/:price/null/:speak?

通过这种方式,您将在if - else语句中寻找这些值来决定要做什么。

© www.soinside.com 2019 - 2024. All rights reserved.