【问题标题】:how to optimize code like reduce code with same functionality如何优化代码,如减少具有相同功能的代码
【发布时间】:2022-01-26 13:11:43
【问题描述】:
let response = {};
var filters = {
  topfeaturedandotherfields: req.body.topfeaturedandotherfields,
};

if (req.body.minprice && req.body.maxprice && req.body.brandName) {
  var filters = {
    $and: [
      { brandName: { $in: req.body.brandName } },
      { topfeaturedandotherfields: req.body.topfeaturedandotherfields },
      { salePrice: { $gte: req.body.minprice, $lte: req.body.maxprice } },
    ],
  };

  var result = await productService.getAllProductofhomepage(
    filters,
    req.body.ordername,
    req.body.orderby
  );
} else {
  if (req.body.minprice && req.body.maxprice) {
    var filters = {
      $and: [
        { topfeaturedandotherfields: req.body.topfeaturedandotherfields },
        { salePrice: { $gte: req.body.minprice, $lte: req.body.maxprice } },
      ],
    };

    var result = await productService.getAllProductofhomepage(
      filters,
      req.body.ordername,
      req.body.orderby
    );
  }
  if (req.body.brandName) {
    var filters = {
      $and: [
        { brandName: { $in: req.body.brandName } },
        { topfeaturedandotherfields: req.body.topfeaturedandotherfields },
      ],
    };

    var result = await productService.getAllProductofhomepage(
      filters,
      req.body.ordername,
      req.body.orderby
    );
  }
}

if (req.body.limit == true)
  var result = await productService.getAllProductofhomepagewithlimit(filters);
else if (req.body.minprice || req.body.maxprice || req.body.brandName) {
} else {
  var result = await productService.getAllProductofhomepage(
    filters,
    req.body.ordername,
    req.body.orderby
  );
}

if (result.length > 0) {
  response = {
    message: "Home page products successfully retrieved",
    error: false,
    data: result,
  };
} else {
  response = {
    message: "Faild to get products",
    error: true,
    data: {},
  };
}

res.status(200).json(response);

此代码用于过滤喜欢查看热门功能和畅销书或最低和最高价格以及此代码中的品牌名称也按订单名称排序,可以是价格或品牌名称或类别,也可以按升序和降序排列订购所以现在你可以看到这段代码就像 if 和 else 但我想优化和减少代码

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    您可以通过动态构建查询条件而不是将逻辑分解为if/else 块来使这个查询变得更好:

    export async function login(req: Request, res: Response): Promise<void> {
        const response = {};
        let filters = {
            topfeaturedandotherfields: req.body.topfeaturedandotherfields,
        };
    
        if (req.body.minprice || req.body.maxprice) {
            const saleCond = { };
            if (req.body.minprice) {
                saleCond.$gte = req.body.minprice;
            }
            if (req.body.maxprice) {
                saleCond.$lte = req.body.maxprice;
            }
            filters.salePrice = saleCond
        }
    
        if (req.body.brandName) {
            filters.brandName = {$in: req.body.brandName}
        }
    
        let result = [];
        if (req.body.limit == true) {
            result = await productService.getAllProductofhomepagewithlimit(filters)
        } else {
            result = await productService.getAllProductofhomepage(filters, req.body.ordername, req.body.orderby);
        }
    
        res.status(200).json({
            message: result.length ? 'Home page products successfully retrieved' : 'Failed to get products',
            error: result.length === 0,
            data: result,
        });
    }
    

    这不仅更加清晰,我们还删除了在此过程中进行的冗余数据库调用。

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 2019-12-14
    • 2021-03-19
    • 2018-03-30
    • 2017-04-19
    • 2022-01-19
    • 2022-08-16
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多