https://github.com/wjdwndtlr/react-site
어제 content 모델에 기본값이 없다는 에러로 마이그레이션과 씨름한 학생입니다.
그 결과, 게시글 (제목, 내용)을 작성하면 잘 post됩니다. 페이로드에 title와 content 각각 잘들어가는거 확인되구요
근데 mysql워크벤처에 들어가면 title만 있고 content는 비어있습니다.
콘솔로그찍어봐도 로그에 타이틀만 기록되구요. 혼자 해결해보려고 계속 씨름해봤는데 도저히 안되서 도움 요청합니다.
- boack=> routes => post.js
const express = require("express");
const db = require("../models");
const router = express.Router();
router.post("/", async (req, res, next) => {
try {
const newPost = await db.Post.create({
title: req.body.title,
content: req.body.content,
UserId: req.user.id
});
// const fullPost = await db.Post.findOne({
// where: { id: newPost.id },
// include: [
// {
// model: db.User
// }
// ]
// });
// console.log("newPost:", newPost);
// console.log("fullPost:", fullPost);
return res.json(newPost);
} catch (e) {
console.error(e);
next(e);
}
});
router.post("/images", (req, res) => {});
router.get("/:id/comments", async (req, res, next) => {});
module.exports = router;
- front=> sagas=> post.js
import { all, fork, takeLatest, put, delay, call } from "redux-saga/effects";
import axios from "axios";
import {
ADD_POST_FAILURE,
ADD_POST_REQUEST,
ADD_POST_SUCCESS,
ADD_COMMENT_FAILURE,
ADD_COMMENT_REQUEST,
ADD_COMMENT_SUCCESS,
LOAD_MAIN_POSTS_FAILURE,
LOAD_MAIN_POSTS_REQUEST,
LOAD_MAIN_POSTS_SUCCESS,
LOAD_HASHTAG_POSTS_REQUEST,
LOAD_HASHTAG_POSTS_SUCCESS,
LOAD_HASHTAG_POSTS_FAILURE,
LOAD_USER_POSTS_SUCCESS,
LOAD_USER_POSTS_FAILURE,
LOAD_USER_POSTS_REQUEST,
LOAD_COMMENTS_SUCCESS,
LOAD_COMMENTS_FAILURE,
LOAD_COMMENTS_REQUEST
} from "../reducers/post";
function addPostAPI(postData) {
return axios.post("/post", postData, {
withCredentials: true
});
}
function* addPost(action) {
try {
const result = yield call(addPostAPI, action.data);
yield put({
type: ADD_POST_SUCCESS,
data: result.data
});
} catch (e) {
yield put({
type: ADD_POST_FAILURE,
error: e
});
}
}
function* watchAddPost() {
yield takeLatest(ADD_POST_REQUEST, addPost);
}
export default function* postSaga() {
yield all([fork(watchAddPost)]);
}
- reducers -> post.js
export default (state = initialState, action) => {
switch (action.type) {
case ADD_POST_REQUEST: {
return {
...state,
isAddingPost: true,
addPostErrorReason: "",
postAdded: false
};
}
case ADD_POST_SUCCESS: {
return {
...state,
isAddingPost: false,
mainPosts: [action.data, ...state.mainPosts],
postAdded: true
};
}
case ADD_POST_FAILURE: {
return {
...state,
isAddingPost: false,
addPostErrorReason: action.error
};
}
default: {
return {
...state
};
}
}
};