project_mern_memories
project_mern_memories copied to clipboard
TypeError: Cannot read property 'length' of undefined
im getting the same error, did you find the answer
you can do something like => if (post?.likes?.length > 0) to get rid of that error
you can do something like => if (post?.likes?.length > 0) to get rid of that error
fix my issue!
thank a lot! <3
I just ran into this error too and your suggested solution has the disadvantage that the likes got reset. If you want to keep your likes after editing change the /server/controllers/posts.js file like this:
export const updatePost = async (req, res) => {
const { id } = req.params;
const { title, message, creator, selectedFile, tags, likes } = req.body;
if (!mongoose.Types.ObjectId.isValid(id)) return res.status(404).send(`No post with id: ${id}`);
const updatedPost = { creator, title, message, tags, selectedFile, likes, _id: id };
await PostMessage.findByIdAndUpdate(id, updatedPost, { new: true });
res.json(updatedPost);
}