Consider this Next.js server action that updates a user's name in the database. What will be the rendered output after the update?
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>; }
Think about what the server action returns and how the component uses it.
The server action returns the string 'Update successful', which the component renders inside a paragraph.
Identify the correct syntax for a server action that deletes a post by ID using Next.js conventions.
Remember to export the function and use the correct 'where' clause syntax.
Option C correctly exports an async function, uses 'where: { id: postId }' to specify the record, and calls revalidatePath.
Examine the server action below. Why does it cause a runtime error when called?
export async function createUser(name) { const user = await db.user.create({ data: { name } }); revalidatePath('/users'); return user; } // Called as createUser(); without arguments
What happens if the function is called without the required argument?
The function expects a 'name' argument. Calling it without arguments makes 'name' undefined, causing the database create to fail or throw a TypeError.
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?
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'.
Check the field names used in the update data object.
The field 'emailAddress' does not exist in the database schema. The update call fails and throws an error, so the email remains unchanged.
Choose the correct statement about how Next.js server actions handle database mutations and cache updates.
Think about where server actions run and how cache is managed in Next.js.
Server actions run on the server, can mutate the database, and require explicit cache invalidation like revalidatePath() to update UI data.