I'm trying to fetch only the users posts, and i'm using this code:
router.get("/:username", async (req, res) => {
try {
const user = await User.findOne({ username: req.params.username });
const posts = await Post.find({ userId: user._id });
res.status(200).json(posts);
} catch (err) {
res.status(500).json(err);
}
});
I'm using Postman for testing GET localhost:6000/posts/admin
and every time I try it, it gives this error
"name": "CastError", "message": "Cast to ObjectId failed for value \"admin\" (type string) at path \"_id\" for model \"Post\""
This is my posts collection in Monogodb
And this is the users collection in Mongodb
I don't know what i'm missing here, I just want the link posts/:username shows the posts of this username only