project_mern_memories icon indicating copy to clipboard operation
project_mern_memories copied to clipboard

TypeError: Cannot read property 'length' of undefined

Open IteshAmbre opened this issue 4 years ago • 5 comments

Screenshot (2)

IteshAmbre avatar Jan 25 '21 12:01 IteshAmbre

im getting the same error, did you find the answer

Aadesh-singh avatar Feb 01 '21 23:02 Aadesh-singh

you can do something like => if (post?.likes?.length > 0) to get rid of that error

iam-amanxz avatar Feb 04 '21 05:02 iam-amanxz

you can do something like => if (post?.likes?.length > 0) to get rid of that error

fix my issue!

Troy-Peng-97 avatar Apr 09 '21 11:04 Troy-Peng-97

thank a lot! <3

ThayGiaoTien avatar May 18 '21 19:05 ThayGiaoTien

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);

}

MrBeech avatar Jul 05 '21 05:07 MrBeech