db.user.findMany() returns a promise, so it must be awaited or handled asynchronously.
Step 2: Fix the handler function
The handler should be async and await the database call before sending the response.
Final Answer:
Missing async/await for the database call -> Option D
Quick Check:
Async db calls need await [OK]
Hint: Always await async database calls in API routes [OK]
Common Mistakes:
Forgetting async keyword on handler
Not awaiting the promise from db calls
Confusing res.status with res.send usage
5. You want to show a list of posts with their authors' names in a Next.js server component. Which approach correctly handles database access to avoid slow page loads?
Option A:
const posts = await db.post.findMany({ include: { author: true } });
return posts.map(p => <div key={p.id}>{p.title} by {p.author.name}</div>);
Option B:
const posts = await db.post.findMany();
const authors = await db.user.findMany();
return posts.map(p => <div key={p.id}>{p.title} by {authors.find(a => a.id === p.authorId).name}</div>);
Option C:
Fetch posts on client side and authors on server side separately.
Option D:
Render posts without author names to speed up loading.
hard
A. Fetch posts and authors separately and match in code
B. Use a single query with include to fetch posts and authors together
C. Split fetching between client and server components
D. Skip author names to improve speed
Solution
Step 1: Analyze database query efficiency
Use a single query with include to fetch posts and authors together, reducing database calls and speeding up loading.
Step 2: Compare other options
Fetch posts and authors separately and match in code makes two queries, which is slower. Split fetching between client and server components causes complexity. Skip author names to improve speed loses important info.
Final Answer:
Use a single query with include to fetch posts and authors together -> Option B
Quick Check:
One query with include = faster load [OK]
Hint: Fetch related data in one query using include [OK]