From the course: Next.js Ecommerce: Build a Shopping Platform from Scratch

Unlock this course with a free trial

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

Create order action

Create order action

All right, guys, so we have the place order page. Now we want to create the action that the form on this page is going to submit to. And I know we don't have the form yet, but we'll do that after. It's just a single button that's going to go over here that's going to call the action. Now, we don't yet have an order actions file. So in lib actions, let's create a new file. And we'll call it order.actions.ts. And at the very top, let's say, use server. And we're going to create an action. It's going to create the order, but it's also going to create the order items. Because remember, we have the order table in our database, and then we also have the order item table. So let's go ahead and export an async function called Create Order, and then we'll go ahead and add our try catch. And now in the error, we're just going to first check for a redirect error with Is Redirect Error, and that's going to be brought in from that. I'm just going to auto import everything, and then we pass in the…

Contents