如何在nodejs中过滤多级类别

ohfgkhjo  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(406)

我正在尝试为多个项目创建一个过滤器,即名称、卖家、价格、类别、品牌。我创建了一个路由,您可以在下面找到它,并为每个过滤器提供了查询参数,除类别外,所有过滤器都正常工作。这是一个多层次的分类。
我可能做错了什么?
这是我的路线

const pageSize = 10;
   const page = Number(req.query.pageNumber) || 1;
   const name = req.query.name || "";
   const slug = req.query.category || "";
   const seller = req.query.seller || "";
   const brand = req.query.brand || "";
   const order = req.query.order || "";
   const min =
      req.query.min && Number(req.query.min) !== 0 ? Number(req.query.min) : 0;
   const max =
      req.query.max && Number(req.query.max) !== 0 ? Number(req.query.max) : 0;
   const rating =
      req.query.rating && Number(req.query.rating) !== 0
        ? Number(req.query.rating)
        : 0;

   const nameFilter = name ? { name: { $regex: name, $options: "i" } } : {};
   const brandFilter = brand ? { brand: { $regex: brand, $options: "i" } } : {};
   const sellerFilter = seller ? { seller } : {};
   const categoryFilter = slug
    ? (category) => category.find((x) => [slug].includes(x.slug))
    : {};
   const priceFilter = min && max ? { price: { $gte: min, $lte: max } } : {};
   const ratingFilter = rating ? { rating: { $gte: rating } } : {};

   const count = await Product.count({
      ...sellerFilter,
      ...nameFilter,
      ...categoryFilter,
      ...priceFilter,
      ...ratingFilter,
      ...brandFilter
    });
    const products = await Product.find({
      ...sellerFilter,
      ...nameFilter,
      ...categoryFilter,
      ...priceFilter,
      ...ratingFilter,
      ...brandFilter
    })
    .populate("seller", "seller.name seller.logo")
    .populate({path: "category", select: "name slug"})
    .sort(sortOrder)
    .skip(pageSize * (page - 1))
    .limit(pageSize);
     res.send({ products, page, pages: Math.ceil(count / pageSize), count });
}))

给出以下数据:;

"products": [
        {
            "category": [
                {
                    "name": "Category Two",
                    "slug": "category-two"
                }
            ],
            "name": "The First Fancy product",
            "seller": {
                "seller": {
                    "name": "John Doe"
                }
            },
            "price": 50000,
            "brand": "newtwo",
            "countInStock": 32,

            "description": "This is the long description which appears in the details screen",
        },
        {
            "category": [
                {
                    "name": "Category One",
                    "slug": "category-one"
                }
            ],
            "name": "Fancy new the second product",
            "seller": {
                "seller": {
                    "name": "Jane Doe"
                }
            },
            "price": 53000,
            "brand": "newone",
            "countInStock": 32,
            "description": "This is the long description which appears in the details screen",
        }
    ],
    "page": 1,
    "pages": 1,
    "count": 2
}
7vux5j2d

7vux5j2d1#

您的categoryfilter必须具有下一个结构:

const categoryArr = (category) => category.find((x) => [slug].includes(x.slug))
const categoryFilter = {}; //Just an Object
if(dataFilter.length > 0){
  categoryFilter = {
    category: { "$in": categoryArr }
  }
}

否则可以通过名称或slug找到

if(dataFilter.length > 0){
  const mappedCategory = categoryArr.map( cat => 
    ({ "$or": [name: cat.name, slug: cat.slug] })
  );

  categoryFilter.category = { "$in": {...mappedCategory}}
}

我暂时无法测试这段代码,但至少它可以指导您如何使用它。
您需要在数组中找到一个对象。那么你必须使用 $in 找到它。然后可以设置是否要搜索对象或对象中的参数。

相关问题