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.

Updating product with server action

Updating product with server action

Now, let's work on updating the product functionality. So I will create the server action to update the product. So in the product actions.js file, I will create a function saying export async function, product and will pass the parameters form data, product id and the existing image. For updating the product, we have the similar logic as the create product action. So I will copy the logic and paste it into the update product action. Now I will modify the logic. First I will assign value to the image path variable while its declaration by saying existing image. Then I will check if the product image is updated or not by modifying if condition. So I will say double ampersand file.size is greater than zero. Now if the product image is updated, then we have to remove the old image from the server And for that I will create an asynchronous function by saying export async function. Handle delete image and will pass parameter image path. Then giving the if condition if image path. Now I…

Contents