From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Deleting product with server action

Deleting product with server action

Now, let's work on delete product functionality. We will start by creating the server action which will delete the product. So in the product actions js file, I will create a function saying export async function delete product and will pass the parameter product. Next I will use db.delete method for deleting the product. So await db.product.delete keeping the where clause id %product.id. Then I will delete the product image from the server by calling await handleDeleteImage and will pass the parameter product.image. And finally, we will call the revalidate method. I'll say revalidate slash products page. Let's save this and integrate this server action. I'll navigate to the product screen component. As you can see, we already have the handle delete function declared. So I will make this function asynchronous by saying async and will call the delete action. State delete product and pass the state selected product. After deleting record, I will close the model. So we'll say set is…

Contents