0
0
NextJSframework~20 mins

Server component database queries in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server Component Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this server component fetching data?

Consider this Next.js server component that fetches user data from a database and renders the user's name.

import { db } from '@/lib/db';

export default async function UserName() {
  const user = await db.user.findFirst({ where: { id: 1 } });
  return <h1>{user?.name ?? 'Guest'}</h1>;
}

If the database has a user with id 1 and name 'Alice', what will this component render?

NextJS
import { db } from '@/lib/db';

export default async function UserName() {
  const user = await db.user.findFirst({ where: { id: 1 } });
  return <h1>{user?.name ?? 'Guest'}</h1>;
}
A<h1>Guest</h1>
B<h1>Alice</h1>
C<h1>undefined</h1>
DError: Cannot read property 'name' of undefined
Attempts:
2 left
💡 Hint

Think about what happens when the user is found and when it is not.

component_behavior
intermediate
2:00remaining
How does this server component behave with async database calls?

Given this Next.js server component:

import { db } from '@/lib/db';

export default async function ProductList() {
  const products = await db.product.findMany();
  return (
    <ul>
      {products.map(p => <li key={p.id}>{p.name}</li>)}
    </ul>
  );
}

What happens if the database query takes a long time?

NextJS
import { db } from '@/lib/db';

export default async function ProductList() {
  const products = await db.product.findMany();
  return (
    <ul>
      {products.map(p => <li key={p.id}>{p.name}</li>)}
    </ul>
  );
}
AThe server component waits for the query before rendering the list.
BThe component renders immediately with empty list and updates later.
CThe component throws an error if the query is slow.
DThe component renders a loading spinner automatically.
Attempts:
2 left
💡 Hint

Remember server components run on the server and can await data before rendering.

🔧 Debug
advanced
2:00remaining
Why does this server component cause a hydration error?

Look at this server component fetching data and rendering a count:

import { db } from '@/lib/db';
import { useState } from 'react';

export default async function Counter() {
  const count = await db.count.findFirst();
  const [value, setValue] = useState(count.value);
  return (
    <div>
      <p>Count: {value}</p>
      <button onClick={() => setValue(value + 1)}>Increment</button>
    </div>
  );
}

Why does this cause a hydration error in Next.js?

NextJS
import { db } from '@/lib/db';
import { useState } from 'react';

export default async function Counter() {
  const count = await db.count.findFirst();
  const [value, setValue] = useState(count.value);
  return (
    <div>
      <p>Count: {value}</p>
      <button onClick={() => setValue(value + 1)}>Increment</button>
    </div>
  );
}
AServer components cannot use React hooks like useState.
BThe database query returns undefined causing a crash.
CThe component is missing a key prop on the button.
DThe async function does not return valid JSX.
Attempts:
2 left
💡 Hint

Think about the difference between server and client components in Next.js.

📝 Syntax
advanced
2:00remaining
Which option correctly fetches data in a Next.js server component?

Choose the correct syntax to fetch all posts from the database in a Next.js server component.

A
export default function Posts() {
  const posts = await db.post.findMany();
  return &lt;ul&gt;{posts.map(p =&gt; &lt;li key={p.id}&gt;{p.title}&lt;/li&gt;)}&lt;/ul&gt;;
}
B
export default async function Posts() {
  const posts = db.post.findMany();
  return &lt;ul&gt;{posts.map(p =&gt; &lt;li key={p.id}&gt;{p.title}&lt;/li&gt;)}&lt;/ul&gt;;
}
C
export default async function Posts() {
  const posts = await db.post.findMany();
  return &lt;ul&gt;{posts.map(p =&gt; &lt;li key={p.id}&gt;{p.title}&lt;/li&gt;)}&lt;/ul&gt;;
}
D
export default function Posts() {
  const posts = db.post.findMany();
  return &lt;ul&gt;{posts.map(p =&gt; &lt;li key={p.id}&gt;{p.title}&lt;/li&gt;)}&lt;/ul&gt;;
}
Attempts:
2 left
💡 Hint

Remember that await can only be used inside async functions.

🧠 Conceptual
expert
2:00remaining
Why should database queries be done in server components in Next.js?

In Next.js, why is it recommended to perform database queries inside server components rather than client components?

ADatabase queries in client components reduce server load by offloading work to the client.
BClient components can perform database queries faster because they run in the browser.
CServer components cannot fetch data, so queries must be done in client components.
DServer components run on the server, so queries are secure and do not expose credentials to the browser.
Attempts:
2 left
💡 Hint

Think about security and where code runs in Next.js.