Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
cómo optimizar el código como reducir el código con la misma funcionalidad
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);

Este código se usa para filtrar, como para ver la función principal y el éxito de ventas o el precio mínimo y máximo y el nombre de la marca también en este código ordenado por nombre de pedido, que podría ser el precio o el nombre de la marca o categoría también en orden ascendente y descendente para que ahora pueda ver este código es como if y else pero quiero optimizar y reducir el código

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede hacer que esta consulta sea mucho más agradable simplemente creando dinámicamente la condición de consulta en lugar de dividir la lógica en bloques 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, }); }

Esto no solo es mucho más claro, sino que solo eliminamos una llamada de base de datos redundante que se realizó en el proceso.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!