app.post('/add', async (요청, 응답) => {
upload.single("img1")(요청, 응답, (err) => {
if (err) return 응답.send('업로드에러')
})
try {
if (!요청.user) {
응답.send("로그인 안함")
}
if (요청.body.title == '') {
응답.send("제목입력안함")
} else {
await db.collection('post').insertOne({
title: 요청.body.title,
content: 요청.body.content,
img: 요청.file ? 요청.file.location : '',
user: 요청.user._id,
username: 요청.user.username
})
console.log("저장 완료")
console.log(요청.body.title)
응답.redirect('/list')
}
} catch (e) {
console.log(e)
}
})
write.ejs
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="main.css">
</head>
<body class="grey-bg">
<%-include('nav.ejs') %>
<form class="form-box" action="/add" method="POST" enctype="multipart/form-data">
<h4>글쓰기</h4>
<input name="title">
<input name="content">
<input type="file" name="img1" accept="image/*" multiple>
<button type="submit">전송</button>
</form>
</body>
</html>
위와 같이 코드를 짰는데 server.js에서 console.log(요청.body.title)해보면 입력한 제목이 제대로 나오는데,
전송버튼 누르면 DB에 title : null, content:null로 저장이 돼있고 list 페이지에서도 제목이랑 내용이 안보입니다ㅠ
원인을 당최 모르겠습니다...