DELETE /books/:id

This will delete a book from the database, the book deleted will be determined by the ID parameter

Endpoint URL

http://127.0.0.1:8080/api/books/:id

Path parameters

Name
Type
Description

id

integer

Unique identifier for each book

Example code

If you are rendering the new releases and favourites and this book is either one of those then you may need to refresh the favourites and newreleases sections

// Add an event listener to delete the book when clicked
deleteButton.addEventListener("click", async () => {
    // Ask if the user is sure they want to delete the book
    const confirmDelete = window.confirm("Are you sure you want to delete this book?");

    // If the user clicked OK, delete the book
    if (confirmDelete) {
        try {
            const response = await fetch(`/api/books/${book._id}`, {
                method: "DELETE",
                headers: {
                    "Content-Type": "application/json",
                },
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            } else {
                books.removeChild(bookContainer); // Remove the book from the library
            const data = await response.text();
                console.log("Success:", data);
            }
        } catch (error) {
            console.error("Error:", error);
        }
    }
});

Example response

Response fields

Name
Type
Description

message

string

A message to confirm or post an error, status of the delete request

Last updated