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

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…

Contents