I am having an error: Cannot read properties of undefined reading((Id))
.
const PostWidget = ({
postId,
postUserId,
name,
description,
location,
picturePath,
userPicturePath,
likes,
comments,
}) => {
const [isComments, setIsComments] = useState(false);
const dispatch = useDispatch();
const token = useSelector((state) => state.token);
const loggedInUserId = useSelector((state) => state.user._id);
const isLiked = Boolean(likes[loggedInUserId]);
const likeCount = Object.keys(likes).length;
const { palette } = useTheme();
const main = palette.neutral.main;
const primary = palette.primary.main;
const patchLike = async () => {
const response = await fetch(`http://localhost:3001/posts/${postId}/like`, {
method: "PATCH",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ userId: loggedInUserId }),
});
const updatedPost = await response.json();
dispatch(setPost({ post: updatedPost }));
};
The error is generated from const isLiked = Boolean(likes[loggedInUsrId])
which checks for a truthy value if an array of userId liked a post before it gets rendered on the home page.
I expected it to show my homepage and its corresponding output/logic but instead got this from my developer console: