0
0
NextJSframework~10 mins

Server action database mutations in NextJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a server action that adds a new user to the database.

NextJS
export const addUser = async (data) => {
  'use server';
  await prisma.user.create({ data: [1] });
};
Drag options to blanks, or click blank then click option'
Adata
Buser
Cinput
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name instead of the function argument.
Forgetting to pass the data property inside the create method.
2fill in blank
medium

Complete the code to mark the server action as a server-only function.

NextJS
export const deleteUser = async (id) => {
  [1];
  await prisma.user.delete({ where: { id } });
};
Drag options to blanks, or click blank then click option'
A'use client'
B'use strict'
C'use server'
D'use action'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'use client' instead, which marks client components.
Omitting the directive entirely.
3fill in blank
hard

Fix the error in the server action that updates a user's email.

NextJS
export const updateUserEmail = async (id, email) => {
  'use server';
  await prisma.user.update({
    where: { id },
    data: { email: [1] }
  });
};
Drag options to blanks, or click blank then click option'
Aid
Bemail
CuserEmail
Ddata.email
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name like id or userEmail.
Trying to access data.email which is undefined here.
4fill in blank
hard

Fill both blanks to create a server action that toggles a user's active status.

NextJS
export const toggleUserActive = async (id, isActive) => {
  'use server';
  await prisma.user.update({
    where: { id: [1] },
    data: { active: [2] }
  });
};
Drag options to blanks, or click blank then click option'
Aid
BisActive
CuserId
DactiveStatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variables like userId or activeStatus.
Mixing up the parameters.
5fill in blank
hard

Fill all three blanks to create a server action that creates a post with title and content.

NextJS
export const createPost = async (title, content) => {
  'use server';
  const newPost = await prisma.post.create({
    data: {
      [1]: [2],
      [3]: content
    }
  });
  return newPost;
};
Drag options to blanks, or click blank then click option'
Atitle
Ccontent
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Using content as a field name instead of body.
Mixing up keys and values.