Express.js:如何使用多个查询字符串正确搜索?

时间:2019-06-07 15:40:36

标签: javascript node.js express query-string

我编写了一个名为filterSearch的函数,该函数不受管理员保护,该函数根据多个搜索/查询参数返回资源。第一次基于状态的搜索效果很好,但是if语句中的其他搜索则无法正常工作。

这是路线

carRouter.get('/car', filterSearch, verifyToken, isAdmin, fetchAllCarAds);

这些分别是SQL查询

export const statusQuery = 'SELECT * FROM cars WHERE status = $1';
export const statusPriceQuery = 'SELECT * FROM cars WHERE status = $1 AND price >= $2 AND price <= $3';
export const statusManufacturerQuery = 'SELECT * FROM cars WHERE status = $1 manufacturer = $2';
export const bodyTypeQuery = 'SELECT * FROM cars WHERE bodytype = $1';
export const statusStateQuery = 'SELECT * FROM cars WHERE status = $1 AND (state = $2 OR state = $3)';

我使用了其他控制语句,例如if,但是仍然无法正常工作。


static async filterSearch(req, res, next) {
    //console.log('req1', typeof req.query);
    if (req.query.status || req.query.bodytype) {
      //console.log('req2', req.query);
      let { status, minprice, maxprice, bodytype, state, manufacturer } = req.query;
      //console.log('req3', typeof bodytype);
      try {
        //status
        status = status.trim().toLowerCase();
        if (status && !minprice && !maxprice) {
          const { rows, rowCount } = await db.query(statusQuery, ['unsold']);
          if (rowCount === 0) {
            return res.status(404).json({
              status: 404,
              error: 'Sorry, this does not exist'
            });
          }
          return res.status(200).json({
            status: 200,
            data: rows
          });
        }

        //status and price
        if (status && minprice && maxprice) {
          minprice = Number(minprice.trim());
          maxprice = Number(maxprice.trim());
          const { rows, rowCount } = await db.query(statusPriceQuery, ['unsold', minprice, maxprice]);
            console.log('row', rowCount);
            if (rowCount === 0) {
              return res.status(404).json({
                status: 404,
                error: 'There is no result for your search now'
              });
            }
            return res.status(200).json({
              status: 200,
              data: rows
            });
          }

        //status and state(new/used)
        if (status && state) {
          state = state.trim().toLowerCase();
          const { rows, rowCount } = await db.query(statusStateQuery, ['unsold', 'new', 'used']);
          if (rowCount === 0) {
            return res.status(404).json({
              status: 404,
              error: 'Search request is unavailable at the moment'
            });
          }
          return res.status(200).json({
            status: 200,
            data: rows
          });
        }

        // status and manufacturer
        if (status && manufacturer) {
          manufacturer = manufacturer.trim().toLowerCase();
          const { rows, rowCount } = await db.query(statusManufacturerQuery, ['unsold', manufacturer]);
          console.log('qui');
            if (rowCount === 0) {
              return res.status(404).json({
                status: 404,
                error: 'Something just happen right now'
              });
            }
            return res.status(200).json({
              status: 200,
              data: rows
            })
          }

        //bodyType
        if (bodytype) {
        //Here I get 'Cannot read property 'trim' of undefined'
          bodytype = bodytype.trim().toLowerCase();
          const { rows , rowCount } = await db.query(bodyTypeQuery, [bodytype]);
          if (rowCount === 0) {
            return res.status(404).json({
              status: 404,
              error: 'your search returned no result'
            });
          }
          return res.status(200).json({
            status: 200,
            data: rows
          });
        }

       return res.status(404).json({
         status: 404,
         error: 'There seems to be an issue with your search'
       });
      } 
      catch(error) {
        return res.status(500).json({
          status: 500,
          error: error.message
        });
      }


    }
    next();
  }
  • 我希望我应该能够根据指定的查询字符串(如果提供)进行搜索,但是当我尝试通过“ bodytype”搜索时,我得到“无法读取未定义的属性” trim”。
  • 当不存在某些查询参数时,应返回“搜索或404似乎有问题”,但它将继续获取所有受管理员保护的汽车。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我要做的就是将函数分解为几个位,以适应所提供的每个查询搜索/术语。