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 user data

Updating user data

Now, let's work on the edit user functionality. So first of all, we will create the server action to edit user. So in the user action JS file, I'll create a function saying export const update user async form data, user ID. Now in the server action, we don't have to mention use server, as we already have used it at the top of this file. Next, let's get the form data. So I'll copy the specific code from add user action and paste it over here. And we'll also copy the code for hashing the password. Alright, now when the edit form is submitted, we'll check if the password is updated or not, that is, if the field is left empty or not. So I will take a condition, if data.password. So if it has a value, then only we'll hash the password, else it won't be hashed. First I'll call the update method by saying await db.adminuser.update where id. Now here we will get the id as a string from the URL params. So we need to use the parseInt to make it an integer. So I'll say parseInt user id. the data…

Contents