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.
Order new first - Next.js Tutorial
From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB
Order new first
All right, guys. So now I just want to order these so that the new is always at the top. So we need to go to the API route, and that's going to be app, API, messages, and then route.js. Not the ID, just route.js directly in messages. And this is the get request right now. So we just really need to add a couple lines here. So first of all, where we get the messages, We're going to change this to read messages. And basically, we want to find where the recipient is the user ID. Let me just close that up. So where the recipient is the user ID, but also where read is true. And then I want to sort. Let's go under or right above the populates. And then let's sort. And we're going to pass in here. We want to sort by, let's say, created at. Because not only do we want the new at the top, but we still want the most recent at the top. So we can say created at and set that to minus 1. And what that'll do is sort read messages in ascending order. OK, now we want to get unread messages. So I'm just…
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)
-