diff --git a/client/src/components/CommentsModule.tsx b/client/src/components/CommentsModule.tsx index 547135f..65d8db2 100644 --- a/client/src/components/CommentsModule.tsx +++ b/client/src/components/CommentsModule.tsx @@ -24,6 +24,11 @@ export default function CommentsModule() { let postId = id + // Function to get the profile picture URL + const getProfilePicture = (userId: string): string => { + return `${config.serverAddress}/users/profile-image/${userId}`; + }; + const getComments = async () => { instance .get(config.serverAddress + `/post/${postId}/getComments`).then((res) => { @@ -47,21 +52,26 @@ export default function CommentsModule() {
{commentList.length > 0 ? ( commentList.map((comment) => { - // console.log(comment); // Log each comment to verify its structure - // // Check if `comment.user` is defined before accessing properties - // const user = comment.user || { firstName: "Unknown", lastName: "" }; + const user = comment.user; // Get the user object from the comment + + if (!user) { + console.error("User object is undefined for comment:", comment); + return null; // Skip rendering this comment if user is undefined + } + + const profilePictureUrl = getProfilePicture(user.id); // Get the user's profile picture return (
-
Name
+
{user.firstName} {user.lastName}
{comment.content}
diff --git a/server/models/Comment.js b/server/models/Comment.js index 7410dd8..eab3511 100644 --- a/server/models/Comment.js +++ b/server/models/Comment.js @@ -22,10 +22,12 @@ module.exports = (sequelize, DataTypes) => { Comment.associate = (models) => { Comment.belongsTo(models.User, { foreignKey: 'userId', + as: "user", onDelete: 'CASCADE', }); Comment.belongsTo(models.Post, { foreignKey: 'postId', + as: "post", onDelete: 'CASCADE', }); Comment.hasMany(models.Comment, { diff --git a/server/routes/post.js b/server/routes/post.js index 298b862..f17b872 100644 --- a/server/routes/post.js +++ b/server/routes/post.js @@ -309,6 +309,7 @@ router.get("/:id/getComments", async (req, res) => { include: [ { model: User, + as: "user", attributes: ["id", "firstName", "lastName"], // Specify the attributes you need }, ],