-
카테고리
-
세부 분야
풀스택
-
해결 여부
미해결
댓글을 달면 500에러가 뜹니다
23.04.10 00:02 작성 조회수 264
0
로그인 후 게시물에 댓글을 달면 500에러가 뜨면서 실패합니다 ㅠㅠ 백엔드에는 comment.comment 없다고 에러 뜨는거같은데 어디가 문제인지 잘모르겠습니다
지금까지 해본건 오타 찾아보고 대소문자를 바꿔보고 했는데도 안되서 결국에 질문하네요 ㅠㅠ
const express = require("express");
const { Post, Image, Comment, User } = require("../models");
const { isLoggedIn } = require("./middlewares");
const router = express.Router();
router.post("/", isLoggedIn, async (req, res, next) => {
try {
const post = await Post.create({
content: req.body.content,
userId: req.user.id,
});
const fullPost = await Post.findOne({
where: { id: post.id },
include: [
{
model: Image,
},
{
model: Comment,
include: [
{
model: User, // 댓글 작성자
attributes: ["id", "nickname"],
},
],
},
{
model: User, // 게시글 작성자
attributes: ["id", "nickname"],
},
{
model: User, // 좋아요 누른 사람
as: "Likers",
attributes: ["id"],
},
],
});
res.status(201).json(fullPost);
} catch (error) {
console.error(error);
next(error);
}
});
router.post("/:postId/comment", isLoggedIn, async (req, res, next) => {
try {
const post = await Post.findOne({
where: { id: req.params.postId },
});
if (!post) {
return res.status(403).send("존재하지 않는 게시글입니다"); //return을 붙여줘야지 send하고 밑에 json이 동시에 실행안됨
}
const comment = await Comment.create({
content: req.body.content,
PostId: req.params.postId,
UserId: req.user.id,
});
const fullComment = await Comment.findOne({
where: { id: comment.id },
include: [
{
model: User,
attributes: ["id", "nickname"],
},
],
});
res.status(201).json(fullComment);
} catch (error) {
console.error(error);
next(error);
}
});
router.delete("/", (req, res) => {
res.json({});
});
module.exports = router;
답변을 작성해보세요.
답변 1