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 - Next.js Tutorial
From the course: Next.js Ecommerce: Build a Shopping Platform from Scratch
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Section intro1m 31s
-
(Locked)
Payment method and Zod schemas9m 27s
-
(Locked)
Payment method page8m 1s
-
(Locked)
Payment method form and update13m 9s
-
(Locked)
Order and OrderItem Prisma models11m 7s
-
(Locked)
Order Zod schemas and types8m 16s
-
(Locked)
Place order page18m 5s
-
(Locked)
Create order action14m 15s
-
(Locked)
Place order form7m 37s
-
(Locked)
Order page and action8m 27s
-
(Locked)
Format utility functions8m 20s
-
(Locked)
Order details table14m 5s
-
(Locked)
-
-
-
-
-
-
-
-
-
-