我有一个MERN web应用程序,我正在实现一些get方法,以便在不同的场合获得相同的项目。现在,当我尝试实现第二个筛选器方法并传递项目模式中的现有参数时,Postman会向我发回一个空数组。[我在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
。