0
0
NextJSframework~20 mins

Server action database mutations in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server Action Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this server action updates a record?

Consider this Next.js server action that updates a user's name in the database. What will be the rendered output after the update?

NextJS
import { revalidatePath } from 'next/cache';

export async function updateUserName(userId, newName) {
  await db.user.update({
    where: { id: userId },
    data: { name: newName }
  });
  revalidatePath('/users');
  return 'Update successful';
}

export default async function Page() {
  const message = await updateUserName(1, 'Alice');
  return <p>{message}</p>;
}
A<p>Update successful</p>
B<p>undefined</p>
C<p>Error: revalidatePath is not a function</p>
D<p>Loading...</p>
Attempts:
2 left
💡 Hint

Think about what the server action returns and how the component uses it.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Next.js server action for deleting a post?

Identify the correct syntax for a server action that deletes a post by ID using Next.js conventions.

A
}
;)'stsop/'(htaPetadilaver  
;)} } dItsop :di { :erehw {(eteled.tsop.bd tiawa  
{ )dItsop(tsoPeteled noitcnuf cnysa tropxe
B
export function deletePost(postId) {
  db.post.delete({ where: { id: postId } });
  revalidatePath('/posts');
}
C
export async function deletePost(postId) {
  await db.post.delete({ where: { id: postId } });
  revalidatePath('/posts');
}
D
async function deletePost(postId) {
  await db.post.delete({ where: { id: postId } });
  revalidatePath('/posts');
}
Attempts:
2 left
💡 Hint

Remember to export the function and use the correct 'where' clause syntax.

🔧 Debug
advanced
2:00remaining
Why does this server action cause a runtime error?

Examine the server action below. Why does it cause a runtime error when called?

NextJS
export async function createUser(name) {
  const user = await db.user.create({
    data: { name }
  });
  revalidatePath('/users');
  return user;
}

// Called as createUser(); without arguments
ANo error, returns a user with empty name
BReferenceError: revalidatePath is not defined
CSyntaxError: Unexpected token 'await'
DTypeError: Cannot read property 'name' of undefined
Attempts:
2 left
💡 Hint

What happens if the function is called without the required argument?

state_output
advanced
2:00remaining
What is the state of the database after this server action runs?

This server action attempts to update a user's email but uses incorrect field names. What is the state of the database after running it?

NextJS
export async function updateUserEmail(userId, newEmail) {
  await db.user.update({
    where: { id: userId },
    data: { emailAddress: newEmail }
  });
  revalidatePath('/users');
  return 'Email updated';
}

// Assume 'email' is the correct field name in the database, not 'emailAddress'.
AThe user's email is updated to newEmail successfully.
BThe user's email remains unchanged; an error is thrown.
CA new user is created with the email newEmail.
DThe database is cleared of all users.
Attempts:
2 left
💡 Hint

Check the field names used in the update data object.

🧠 Conceptual
expert
2:00remaining
Which statement about Next.js server actions and database mutations is true?

Choose the correct statement about how Next.js server actions handle database mutations and cache updates.

AServer actions run on the server, perform database mutations, and can trigger cache invalidation with revalidatePath().
BServer actions automatically update the UI without needing revalidation or cache control.
CServer actions run on the client and directly mutate the database without cache updates.
DServer actions cannot perform database mutations; they only fetch data.
Attempts:
2 left
💡 Hint

Think about where server actions run and how cache is managed in Next.js.