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.

Implementing delete product feature

Implementing delete product feature

Now, let's implement the delete functionality where on clicking the delete button, the respective product is deleted. So let's create a server action where we have the code defined for executing the delete query. I'll import the better SQLite 3 and create the instance by saying import SQLite from better SQLite 3 const db is equal to SQLite products dot SQLite. And let's create the server action. I'll write async function, delete product, use server, db dot prepare will give the query delete from products where id question mark and will attach with run method. Now we will pass the product ID as an argument. So here I will have the parameter product ID and will pass product ID as an argument to the run method. Now we want to call this on click of the delete button. So I will define on click attribute and return the delete product method and pass the product ID in it. So I'll say onClick, the method, delete product and passing the product.id. Let's save and check. And we get the error…

Contents