Exercise 17: Deleting a Record

In this exercise, we will be introducing the last vital Knex method to complete our Create, Read, Update, Delete (CRUD) journey, .delete():

  1. Let's add a route for deleting a todo item:
{
method: 'DELETE',
path: '/todo/{todoId}/item/{id}',
handler: async (request, reply) =>
{
const id = request.params.id;
const deleted = await Knex('todo_item')
.where('id', id)
.delete();
return reply({ message: 'deleted' });
},
},
  1. Now, let's add one more item on our previous todo (of ID 1), then delete it:
    1. Add item:
    1. Now that we have its ID (2, in this case), delete it:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset