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.

Fetch data using server component

Fetch data using server component

So now we want to start to fetch our properties from the database. And we're going to start with the properties page here. All right, so the way this works is we're going to create an API route to use Mongoose to get the properties from the database. And then from our front end, right from this component, we're going to then fetch from our API route and then display the properties. So let's start with the API route. So we'll go to app API properties route JSX. It's still this. We're making a GET request to slash API slash properties. In fact, what I'll usually like to do is put a comment here with exactly what this route is so we know. So this would be slash properties. Now, we're already connecting to the database. So all we have to do, really, is just add a line of code here saying const properties. Set that to a wait. And now, since we brought in, or we didn't bring it in yet, but we will right now, the property model. So we want to bring in property from models property. And then…

Contents