728x90
요약: 커뮤니티 삭제 기능
일시: 23.09.14
장소: 더휴먼컴퓨터아트아카데미
배운 점:
삭제 기능
import { connectDB } from "@/util/database";
import { ObjectId } from "mongodb";
export default async function handler(req, res) {
const body = req.body
// DELETE 요청 시 실행
if (req.method === "DELETE") {
try {
// 로그인한 유저한 동일한 email의 정보를 가져오기
const db = (await connectDB).db("forum"); // connectDB()를 호출하여 DB 연결을 얻습니다.
const result = await db
.collection("post")
.deleteOne({ _id: new ObjectId(body.id) });
// 성공 시 결과 전달
return res.status(200).json(result);
} catch (error) {
console.error("Error fetching data:", error);
return res.status(500).json({ error: "Internal server error" });
}
} else {
// 요청 메소드가 다를 때 에러 메시지 전송
return res.status(405).json({ error: "Method not allowed" });
}
}
부족한 점:
728x90
'TIL > Today I Learned' 카테고리의 다른 글
[TIL] 23.09.18 Today I Learned / 마이페이지 제작 1 (0) | 2023.09.19 |
---|---|
[TIL] 23.09.15 Today I Learned / fixed menu 제작 (0) | 2023.09.19 |
[TIL] 23.09.13 Today I Learned / 커뮤니티 수정페이지 (0) | 2023.09.15 |
[TIL] 23.09.11 Today I Learned / 타입스크립트 강의 (0) | 2023.09.11 |
[TIL] 23.09.10 Today I Learned / email 중복 체크 1차 구현 (0) | 2023.09.11 |