繁体   English   中英

对我的 express 后端 [MERN (React + Express)] 的多个过滤获取请求

[英]Multiple filtered get request to my express backend [MERN (React + Express)]

我有一个 MERN web 应用程序,我正在实施一些 get 方法来在不同场合获取相同的项目。 现在,当我尝试实现第二个过滤器方法并在我的项目模式中传递现有参数时,Postman 将返回一个空数组。 [我在第一种方法GET -> HTTP://localhost:5001/api/rooms/THE_USER_ID的参数]

有可能做这样的事情吗?

 //@action GET api/rooms/:where //@descr GET filtered search rooms //@access Public router.get("/:where", (req, res) => { Room.find({ where: req.params.where, }).then((room) => res.json(room)).catch((err) => res.status(404).json({ success: false })); }); //@action GET api/rooms/:user_id //@descr GET filtered rooms for user dashboard //@access Private router.get("/:user_id", auth, (req, res) => { Room.find({ user_id: req.params.user_id, }).then((room) => res.json(room)).catch((err) => res.status(404).json({ success: false })); });

在我的情况下,第二个 Room.find 必须是私有的,但我尝试公开它,它也不起作用。 我也尝试更改参数,但它不起作用。 有人可以帮我吗?

想象一下你是路由器。 您会看到一个请求: /api/rooms/1 你怎么知道要调用哪个处理程序 - 1是指:where还是:user_id 它们具有相同的路由模式 - 都听/api/rooms/<something>

您应该有两条单独的路线,以便可以将它们区分开来 - 例如, /api/rooms/by-where/:where/api/rooms/by-user/:user_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM