글작성시 userId가 데이터에 저장을 안해서 게시글을 가져올려고 해도 안가져와집니다.
428
작성한 질문수 46
1.
뭔가 프론트는 문제가 없는거같은데
백에서는 어디에서 문제가 있는지 잘모르겠습니다 ㅠㅠ


에러는 없습니다
3.
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) => {
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) => {
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,
});
res.status(201).json(comment);
} catch (error) {
console.error(error);
next(error);
}
});
router.delete("/", (req, res) => {
res.json({});
});
module.exports = router;
import { Form, Input, Button } from "antd";
import { useCallback, useRef, useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { ADD_POST_REQUEST } from "../reducers/post";
import useInput from "./hooks/useInput";
const PostForm = () => {
const { imagePaths, addPostDone, addPostLoading } = useSelector(
(state) => state.post
);
const [text, setText] = useState("");
const dispatch = useDispatch();
const imageInput = useRef();
useEffect(() => {
if (addPostDone) {
setText("");
}
}, [addPostDone]);
const onSubmitForm = useCallback(() => {
dispatch({
type: ADD_POST_REQUEST,
data: text,
});
}, [text]);
const onChangeText = useCallback((e) => {
setText(e.target.value);
}, []);
const onClickImageUpload = useCallback(() => {
imageInput.current.click();
}, [imageInput.current]);
return (
<Form
style={{ margin: "10px 0 20px" }}
encType="multipart/form-data"
onFinish={onSubmitForm}
>
<Input.TextArea
value={text}
onChange={onChangeText}
maxLength={140}
placeholder="어떤 일이 생겼나요"
/>
<div>
<input type="file" multiple hidden ref={imageInput} />
<Button onClick={onClickImageUpload}>이미지 업로드</Button>
<Button
type="primary"
style={{ float: "right" }}
htmlType="submit"
loading={addPostLoading}
>
짹쨱
</Button>
</div>
<div>
{imagePaths.map((v) => (
<div key={v} style={{ display: "inline-block" }}>
<img src={v} style={{ width: "200px" }} alt={v} />
<div>
<Button>제거</Button>
</div>
</div>
))}
</div>
</Form>
);
};
export default PostForm;
//saga - AddPost
function addPostAPI(data) {
return axios.post("/post", { content: data });
}
function* addPost(action) {
try {
const result = yield call(addPostAPI, action.data);
yield put({
//put : dispatch
type: ADD_POST_SUCCESS,
data: result.data,
});
yield put({
type: ADD_POST_TO_ME,
data: result.data.id,
});
} catch (err) {
yield put({
type: ADD_POST_FAILURE,
data: err.response.data,
});
}
}
// reducer
case ADD_POST_REQUEST:
draft.addPostLoading = true;
draft.addPostDone = false;
draft.addPostError = null;
break;
case ADD_POST_SUCCESS:
draft.addPostLoading = false;
draft.addPostDone = true;
draft.mainPosts.unshift(action.data);
break;
case ADD_POST_FAILURE:
draft.addPostLoading = false;
draft.addPostError = action.error;
break;
답변 1
넥스트 버젼 질문
0
78
2
로그인시 401 Unauthorized 오류가 뜹니다
0
89
1
무한 스크롤 중 스크롤 튐 현상
0
175
1
특정 페이지 접근을 막고 싶을 때
0
103
2
createGlobalStyle의 위치와 영향범위
0
96
2
인라인 스타일 리렌더링 관련
0
91
2
vsc 에서 npm init 설치시 오류
0
146
2
nextjs 15버전 사용 가능할까요?
0
158
1
화면 새로고침 문의
0
121
1
RTK에서 draft, state 차이가 있나요?
0
153
2
Next 14 사용해도 될까요?
0
452
1
next, node 버전 / 폴더 구조 질문 드립니다.
0
349
1
url 오류 질문있습니다
0
211
1
ssh xxxxx로 우분투에 들어가려니까 port 22: Connection timed out
0
373
1
sudo certbot --nginx 에러
0
1275
2
Minified React error 콘솔에러 (hydrate)
0
470
1
카카오 공유했을 때 이전에 작성했던 글이 나오는 버그
0
247
1
프론트서버 배포 후 EADDRINUSE에러 발생
0
327
1
npm run build 에러
0
518
1
front 서버 npm run build 중에 발생한 에러들
0
382
1
서버 실행하고 브라우저로 들어갔을때 404에러
0
338
2
css 서버사이드 랜더링이 적용되지 않아서 문의 드립니다.
0
288
1
팔로워 3명씩 불러오고 데이터 합쳐주는걸로 바꾸고 서버요청을 무한으로하고있습니다.
0
239
2
해시태그 검색에서 throttle에 관해 질문있습니다.
0
201
1





