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 - Next.js Tutorial
From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB
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…
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)
-