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 - Next.js Tutorial
From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB
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…
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)
Create MongoDB database8m 44s
-
(Locked)
MongoDB Compass and importing data4m 36s
-
(Locked)
Database connection and Mongoose6m 31s
-
(Locked)
Your first API route6m 5s
-
(Locked)
Property and user models12m 30s
-
(Locked)
Fetch data using server component9m 48s
-
(Locked)
Requests file4m 25s
-
(Locked)
Fetch single property10m 51s
-
(Locked)
Single property page11m 54s
-
(Locked)
Property details component15m 34s
-
(Locked)
Spinner component2m 37s
-
(Locked)
-
-
-
-
-
-
-