(강의랑 똑같이 따라했어요!)
app.post("/write", async (req, res) => {
upload.single("image")(req, res, (err) => {
if (err) return res.send("업로드 에러");
});
console.log(req.body); // 여기서 req.body 내용이 [Object: null prototype] {} 이렇게 나옴
const username = req.body.username;
const title = req.body.title;
const content = req.body.content;
// prettier-ignore
await db.collection("tweet").insertOne({
title: title,
content: content,
author: username,
// img: req.file ? req.file.location : "", // 이미지 업로드 완료시 S3가 생성해주는 이미지의 URL 받아오기
view: 0,
like: 0,
date: new Date()
});
res.send(`게시글을 작성하였습니다.`);
});
코드를 강의 그대로 똑같이 했는데 req.body가 비어서 게시글 필드들도 아래와 같이 null로 입력됩니다.
API내부에 넣지 않고 다시 미들웨어로 아래처럼 작성하면 올바로 작동합니다.
app.post("/write", upload.single("image"), async (req, res) => {
이유가 뭔지 궁금합니다!