From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB
Unlock this course with a free trial
Join today to access over 25,200 courses taught by industry experts.
Delete messages - Next.js Tutorial
From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB
Delete messages
Now, we want to be able to delete messages. Right now, we can mark them as new. We can mark them as read. But we also want to be able to get rid of them completely. So let's start off by creating the delete API route. So this is going to be in your app slash API slash messages, and then ID, and then route JS, because we need to get the specific ID of the one we want to delete. So what I'm going to do is just copy the put that we created. and paste that in here. And then we're going to change this and this to delete. And then let's see, we're going to be doing a lot of the same stuff. We want to connect to the database. We want to get the ID. And then we also want to get the user ID from the session. We want to find the message that we're going to delete, check if it exists, verify ownership, obviously, because we don't want just anybody to delete anyone's message. And then instead of updating like we did here, we're going to get rid of both of these. We don't want to call save. We…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
-
(Locked)
Message model and form state12m 59s
-
(Locked)
Message submit and API route14m 22s
-
(Locked)
Require auth for contact form3m 53s
-
(Locked)
Fetch messages10m 30s
-
(Locked)
Display messages11m 15s
-
(Locked)
Mark as read15m 19s
-
(Locked)
Delete messages8m 38s
-
(Locked)
Order new first3m 25s
-
(Locked)
New message count display11m 5s
-
(Locked)
Global context for unread messages11m 6s
-
(Locked)
-