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.

New message count display

New message count display

OK, so now I want to make it so this thing in the nav bar tells us how many messages we have. So to do that, we're going to have an API route that we hit that will basically count the unread messages and send it back. So in the app API folder, we're going to go to Messages, and we're going to create a new folder called unread-count. And then we're going to create a file in there called route.js. OK, and then I'm just going to copy from one of these other ones, I guess from the message ID, route.js. Let's see, is there a GET request here? No, because we're not getting single. I guess I'll just copy the PUT and the IMPORTS. And then let's paste that in here. So we do need all three of these, the connectDB, the message model, and getSessionUser. We're going to keep this export. And then let's change these to get. And it's going to be, for the route, api slash messages slash unread dash count. So we want to get the database or connect to the database. We don't need this because we're not…

Contents