I am getting the following error when trying to handle post requests:
TypeError: Cannot read property 'forEach' of undefined
Please let me know what I am doing wrong. This is my code:
router.post("/", validateToken, async (req, res) => {
const { post, newTags } = req.body;
console.log("my body!", req.body);
post.fullName = req.user.fullName;
post.UserId = req.user.id;
await Posts.create(post);
newTags.forEach((tag) => {
tagPost(tag, id);
});
res.json(post);
});
Thanks.
console.log("my body!", req.body);bodydoes in fact have anewTagsproperty? Seems it doesn't.newTagsproperty in the body you posted in the comment. OnlypostTextandpostTitle... So obviouslynewTagswill be undefined after deconstructing the body ... Which makes me wonder whypost.fullNamedoesn't throw an error, becausepostwill also be undefined