Tutorial 4 - Deleting documents
Your event app currently allows you to create new events, each of which stores a name, description, date, time, and cost data, in separate now editable fields.
In this tutorial, you will extend the existing blueprint and event app functionality (utilising the JS SDK) to add event deletion capabilities, which are useful for events that have passed.
This tutorial takes around 15-25 minutes to complete.
Define a DELETE method for event documents
A DELETE request will be defined in your API specification to allow any values of properties for an existing event (passed in the body of such an HTTP request), to be updated.
To define this method:
-
In the
event-app/api/
directory, open theapi.yaml
file. -
Copy YAML snippet (below), and paste it to the end of your
api.yaml
file, immediately under thepatch:
definition you copy and pasted in the previous tutorial, and ensure thatdelete:
lines up with thepatch:
definition above it.delete: description: Delete an event by ID x-datastore-acl: public responses: '204': description: The event is deleted
You should now see that this additional YAML snippet defines the DELETE request on the
/events/{eventid}
endpoint of your API specification.The DELETE request to (and response from) the
/events/{eventid}
endpoint deletes the event specified byeventid
, and returns an HTTP status value of 204 upon success. -
Save the updated
api.yaml
file.
Copy the 'tutorial 4' files over to your event-app
directory
Now copy the next level of your app’s functionality (which handles your API changes above) from the source tutorial repository over to your event-app
directory.
-
From the
event-app
directory, copy the content of thestep5
directory to theevent-app
directory:$ cp -r ../tutorial/step5/*.* .
-
Refresh or re-open the initial
index.html
file in theevent-app
directory.
Test the updated app
Notice that each existing event you had previously created now has a Delete link (next to its Edit link).
-
Click a Delete link to the right of an existing event.
-
In the resulting Delete event dialog box, click Delete event to confirm the event’s deletion.
Examine the deleteEvent
function in the main.js
file
In the event-app
directory, open the main.js
file and examine the const deleteEvent …
function definition towards the middle of the file.
The following code snippet shows the JS SDK call to delete an existing event document from the an events collection.
const deleteEvent = () => {
if (deletingEvent !== null) {
datastore.collection('events').doc(deletingEvent).delete().then(() => { (1)
...
}).catch((error) => { (2)
printError('#deleteEvent .modal-body', error);
});
}
};
1 | The JS SDK call to delete an existing event document (specified by its ID) from your Datastore service. The datastore.collection() method sets the top-level collection (i.e. events initially defined in tutorial 1) as an object.The doc() method is then called on this collection() object to prepare a document request object for the existing event document, specified by its ID.
This ID value is retrieved from your app’s deletingEvent value.The delete() method is then called on the prepared document request object to delete this event document (whose ID is deletingEvent ).
This method uses the API’s delete definition within /events/{eventid} , also initially defined in tutorial 1, to delete the event document (specified by your app’s deletingEvent value).Note: deletingEvent is an event ID, whose value is set elsewhere in main.js . |
2 | The JavaScript function call to handle the error response from the Datastore service, if the delete() method does not return an HTTP status value of 204.
The error response from the Datastore service is temporarily held by error , which the app then outputs. |
Now that you understand how to define API endpoints to delete an existing a document using the JS SDK, you can now extend the functionality of your event app to handle the ability to edit your data.