NodeJS – TypeScript – 如果有多个参数,URL查询不起作用

huangapple go评论71阅读模式
英文:

NodeJS - TypeScript - URL Query doesn't work if multiple parameters

问题

以下是您要翻译的内容:

我在我的路由中有以下控制器:

export const getHotels = async (
  req: Request,
  res: Response,
  next: NextFunction
) => {
  try {
    const hotels = await Hotel.find(req.query).limit(+req.query.limit);
    res.status(200).json(hotels);
  } catch (err) {
    next(err);
  }
};

在我的数据库中,我有带有特色属性(布尔值)的酒店,我使用Mongoose检索并使用其limit方法缩小结果。

我注意到,无论我调用(GET):/api/hotels?featured=true&limit=1,如果我有多个参数,查询都会返回一个空数组。

如果控制器是await Hotel.find().limit(+req.query.limit);,并且URL是/api/hotels?limit=1,它可以正常工作。

或者如果控制器是await Hotel.find(req.query);,URL是/api/hotels?featured=true,也可以正常工作。

有人知道可能是什么问题吗?非常感谢。

英文:

I have the following controller in my route:

export const getHotels = async (
  req: Request,
  res: Response,
  next: NextFunction
) => {
  try {
    const hotels = await Hotel.find(req.query).limit(+req.query.limit);
    res.status(200).json(hotels);
  } catch (err) {
    next(err);
  }
};

In my database, I have hotels with a featured property (boolean) that I retrieve with Mongoose and reduce the results with its limit method.

I noticed that my query returns an empty array if I have several parameters no matter what if I call (GET): /api/hotels?featured=true&limit=1

It works fine if the controller is await Hotel.find().limit(+req.query.limit); and the URL /api/hotels?limit=1

Or if controller is await Hotel.find(req.query); and URL /api/hotels?featured=true

Does anyone have an idea of what could be the issue? Many thanks.

答案1

得分: 1

当您的GET请求为/api/hotels?featured=true&limit=1时,req.query内容是:

{
  featured: "true",
  limit: "1"
}

因此,Hotel.find(req.query)查找具有“featured”字段为“true”且“limit”字段为“1”的文档... 因此它找不到任何内容。

您可以确保只提取必要的字段,例如使用Lodash pick 实用程序:

Hotel
  .find(pick(req.query, ["featured"]))
  .limit(+req.query.limit)
英文:

When your GET request is /api/hotels?featured=true&limit=1, the req.query content is:

{
  featured: "true",
  limit: "1"
}

...therefore the Hotel.find(req.query) looks for documents with both "featured" field as "true" AND "limit" field as "1"... hence it does not find anything.

You could make sure to extract only necessary fields, e.g. with Lodash pick utility:

Hotel
  .find(pick(req.query, ["featured"]))
  .limit(+req.query.limit)

答案2

得分: 0

我之前没有在使用 find() 前使用对象括号,这就是为什么 MongoDB 只能接受第一个参数的原因。

示例:

const { limit, ...others } = req.query;
const hotels = await Hotel.find({
  ...others,
}).limit(+limit);

通过这样做,我可以在我的 GET 请求中添加任何参数,而无需使用 Lodash 选择特定的参数。

英文:

I realised I wasn't using the object bracket with find() before which is why mongoDB could only take the 1st argument.

Example:

const { limit, ...others } = req.query;
    const hotels = await Hotel.find({
      ...others,
    }).limit(+limit);

By doing so, I can add any parameter in my GET request and don't need to use Lodash to pick a specific parameter.

huangapple
  • 本文由 发表于 2023年1月4日 12:39:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75001018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定