Complete the code to define a server action that adds a new user to the database.
export const addUser = async (data) => {
'use server';
await prisma.user.create({ data: [1] });
};The data object contains the user information to be saved. It must be passed to prisma.user.create inside the data property.
Complete the code to mark the server action as a server-only function.
export const deleteUser = async (id) => {
[1];
await prisma.user.delete({ where: { id } });
};The directive 'use server' marks the function as a server action in Next.js.
Fix the error in the server action that updates a user's email.
export const updateUserEmail = async (id, email) => {
'use server';
await prisma.user.update({
where: { id },
data: { email: [1] }
});
};id or userEmail.data.email which is undefined here.The email parameter holds the new email value and should be assigned directly.
Fill both blanks to create a server action that toggles a user's active status.
export const toggleUserActive = async (id, isActive) => {
'use server';
await prisma.user.update({
where: { id: [1] },
data: { active: [2] }
});
};userId or activeStatus.The id parameter identifies the user, and isActive is the new active status to set.
Fill all three blanks to create a server action that creates a post with title and content.
export const createPost = async (title, content) => {
'use server';
const newPost = await prisma.post.create({
data: {
[1]: [2],
[3]: content
}
});
return newPost;
};content as a field name instead of body.The database expects the fields title and body. We assign the title parameter to the title field, and content parameter to the body field.