Skip to content

Commit

Permalink
Fixed poll and comment bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tuhinexe committed Apr 7, 2023
1 parent c90571a commit 6cda5d6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/api/commentAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const addComment = async (commentDetails, postId) => {
throw new Error(error);
});
} catch (err) {
console.log(err);
throw new Error(err);
}
};

Expand Down
6 changes: 6 additions & 0 deletions backend/controllers/commentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const addCommentController= async (req,res) =>{
commentor: req.user._id
}
const postId = req.params.postId

if(commentDetails.content.trim() === ""){
req.flash("error","cannot add empty comment")
res.redirect(`/post/${postId}`)
return
}

try{
await commentAPI.addComment(commentDetails,postId)
Expand Down
5 changes: 5 additions & 0 deletions backend/controllers/pollController.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const createPollsController = async (req, res) => {
const id = req.user._id;
const title = req.body.title;
const [option1, option2, option3, option4] = req.body.option;
if(title.trim() === "" || option1.trim() === "" || option2.trim() === "" || option3.trim() === "" || option4.trim() === ""){
req.flash("error", "can't create poll with empty fields");
res.redirect("/");
return;
}
const newPoll = new pollsModel({
creatorId: id,
content: title,
Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/postController.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const getSinglePostRender = async (req, res) => {
pagename: "comment",
profilePic: req.user.profilePic_url,
};
res.render("post", { pageInfo: pageInfo, post: post, comments: comments });
res.render("post", { pageInfo: pageInfo, post: post, comments: comments, messages: req.flash() });
} catch (err) {
res.redirect("/");
}
Expand Down
5 changes: 5 additions & 0 deletions backend/views/post.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@
</div>

</section>

<% if(messages.error){ %>
<%- include ('partials/flashModal',{messages: messages}) %>
<% } %>
<%- include ('partials/footer') %>

0 comments on commit 6cda5d6

Please sign in to comment.