簡體   English   中英

Express JS Middleware Mongoose 過濾器和排序查詢我沒有得到我期望得到的結果,它沒有排序

[英]Express JS Middleware Mongoose filter and sort query i dont get what i expect to get and it's not sorting

這是過濾和排序的中間件我試圖修改這個中間件,但它不能像我想要的那樣工作

 const aliasTopMenu = (req, res, next) => {
  req.query.sort = '-price';
  req.query.fields = 'name,price,rating';
  req.query.limit = '3';
  next();
};

在路由器文件中,只需在函數之前傳遞中間件即可獲取數據 express.Route().route('/top-5-menu').get(aliasTopMenu, getAllItems);

getAllItems 函數:此函數用於獲取所有數據並根據用戶需要過濾一些數據

const getAllItems = AsyncWrapper(async (req, res) => {
  //1)filtring

  const qeuryObj = { ...req.query };
  const excludField = ['page', 'sort', 'limit', 'fields'];
  excludField.forEach((el) => {
    delete qeuryObj[el];
  });
  //advanced filtring
  let queryStr = JSON.stringify(qeuryObj);
  queryStr = queryStr.replace(/(gt|gte|lte|lt)/g, (matched) => {
    return `$${matched}`;
  });
  let query = await Item.find(JSON.parse(queryStr));

  //2) sorting

  if (req.query.sort) {
    const sortBy = req.query.sort.split(',').join(' ');
    query = await Item.find(JSON.parse(queryStr)).sort(sortBy);
  }
  //3)Field limiting
  if (req.query.fields) {
    const fields = req.query.fields.split(',').join(' ');

    query = await Item.find(JSON.parse(queryStr)).select(fields);
  }
  //Pagination page and limit
  if (req.query.page) {
    const page = req.query.page * 1 || 1;
    const limit = req.query.limit * 1 || 100;
    const skip = (page - 1) * limit;
    const numItems = await Item.countDocuments();
    if (skip >= numItems) {
      throw new Error('this page do not exist');
    }
    query = await Item.find(JSON.parse(queryStr)).skip(skip).limit(limit);
  }

  //execute query

  const items = await query;

  res.status(200).json(items);
});

對此查詢 "{{URL}}/items/top-5-menu" {{URL}} 的結果是包含 localhost 和 prot 的全局變量

 [
    {
        "rating": 4.5,
        "_id": "62aa6f0cb6cf327c8c4d219a",
        "name": "Pizza",
        "price": 350
    },
    {
        "rating": 4.5,
        "_id": "62aa6f15b6cf327c8c4d219c",
        "name": "Sawarma",
        "price": 150
    },
    {
        "rating": 4.5,
        "_id": "62acf53798c9c74218de901f",
        "name": "Burger",
        "price": 250
    },
    {
        "rating": 4.5,
        "_id": "62acf56298c9c74218de9021",
        "name": "Pizza",
        "price": 400
    },
    {
        "rating": 4.5,
        "_id": "62ad10569b344b2b223e6ea4",
        "name": "Shawarma",
        "price": 500
    },
    {
        "rating": 4.5,
        "_id": "62ad10709b344b2b223e6ea6",
        "name": "takos",
        "price": 400
    },
    {
        "rating": 4.5,
        "_id": "62ad10769b344b2b223e6ea8",
        "name": "takos",
        "price": 250
    },
    {
        "rating": 4.5,
        "_id": "62ad10839b344b2b223e6eaa",
        "name": "chiken",
        "price": 400
    },
    {
        "rating": 4.5,
        "_id": "62ad108f9b344b2b223e6eac",
        "name": "chiken",
        "price": 350
    },
    {
        "rating": 4.5,
        "_id": "62ad109b9b344b2b223e6eae",
        "name": "chiken",
        "price": 350
    }
]
 

不要按價格對數據進行排序

問題就在這里,看看我是如何使用 await 關鍵字的,但是當我嘗試它時,它正在工作,希望對遇到此類錯誤的人有所幫助

 let query = await Item.find(JSON.parse(queryStr))
query = await Item.find(JSON.parse(queryStr)).sort(sortBy);
query = await Item.find(JSON.parse(queryStr)).select(fields);
query = await Item.find(JSON.parse(queryStr)).skip(skip).limit(limit);

解決方案是這樣的:

let query = Item.find(JSON.parse(queryStr))
  query = query.sort(sortBy);
    query = query.select(fields);
    query = query.skip(skip).limit(limit);

比你讓所有的改變等待

const items = await query;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM