0
0
NextJSframework~20 mins

Why database access matters in NextJS - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Database Access Mastery in Next.js
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do Next.js Server Components prefer direct database access?

In Next.js, Server Components can fetch data directly from a database. Why is this approach preferred over fetching data via client-side API calls?

ABecause client-side API calls are faster and more secure than server-side database access.
BBecause Server Components cannot fetch data from APIs, only databases.
CBecause Server Components run on the server, they can securely access the database without exposing credentials to the client.
DBecause direct database access on the client improves SEO and performance.
Attempts:
2 left
💡 Hint

Think about where the code runs and what information should stay secret.

component_behavior
intermediate
2:00remaining
What happens if a Next.js Client Component tries to access the database directly?

Consider a Next.js Client Component that attempts to import a database client and query data directly. What will happen when this component runs in the browser?

AIt will successfully fetch data because the browser can connect to databases directly.
BIt will throw a runtime error because database clients cannot run in the browser environment.
CIt will silently fail and return empty data without errors.
DIt will fetch data but expose database credentials to the user.
Attempts:
2 left
💡 Hint

Remember where client components run and what environment they have.

state_output
advanced
2:00remaining
What is the rendered output of this Next.js Server Component with database access?

Given the following Next.js Server Component code that fetches user names from a database, what will be rendered?

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

export default async function UserList() {
  const users = await db.user.findMany({ select: { name: true } });
  return (
    <ul>
      {users.map(user => <li key={user.name}>{user.name}</li>)}
    </ul>
  );
}
A<ul><li>Alice</li><li>Bob</li><li>Carol</li></ul>
B<ul><li>undefined</li><li>undefined</li></ul>
C<ul></ul>
DRuntime error: db.user.findMany is not a function
Attempts:
2 left
💡 Hint

Assume the database has users named Alice, Bob, and Carol.

📝 Syntax
advanced
2:00remaining
Which option correctly uses Next.js Server Actions to update a database record?

Next.js Server Actions allow server-side functions to be called from client components. Which code snippet correctly defines a Server Action to update a user's name in the database?

A
export async function updateUserName(id, newName) {
  await fetch('/api/updateUser', { method: 'POST', body: JSON.stringify({ id, newName }) });
}
B
export const updateUserName = async (id, newName) =&gt; {
  await db.user.update({ where: { id }, data: { name: newName } });
};
C
export function updateUserName(id, newName) {
  db.user.update({ where: { id }, data: { name: newName } });
}
D
export async function updateUserName(id, newName) {
  await db.user.update({ where: { id }, data: { name: newName } });
}
Attempts:
2 left
💡 Hint

Server Actions must be async functions that perform server-side logic directly.

🔧 Debug
expert
3:00remaining
Why does this Next.js Server Component fail to fetch data from the database?

Examine the following Next.js Server Component code. It throws an error when trying to fetch data. What is the most likely cause?

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 function is not async, so awaiting the database call is missing, causing products to be a Promise, not an array.
BThe import path '@/lib/db' is incorrect and causes a module not found error.
CThe map function is used incorrectly; it should be forEach instead.
DThe database client does not support findMany method.
Attempts:
2 left
💡 Hint

Consider how async database calls work in JavaScript functions.