07 - Update an Item
In this module, we'll update the item that is returned from the GET request and displayed in the input field.
CODE
Add the following code to your 03-auth-test.js
file.
We get the value of the input provided from the user for both the
updateNumber
andupdateValue
fields and assign each to a variable.Like before, we pass in the input from the user to the url with a template literal.
We create an object that packages up our request. We capture the value of
authTestDataInput
and store it in the variableauthInputData
variable.We are doing an update method, so this will be a
PUT
request.Just like we did in past
POST
methods, we use thestringify
method to convert the object to a JSON object.We print the response to our fetch to the console.
We make a reference to the
<label>
in step 12 (Update Single Item), then set its value to the data we put in the database.We run the
getall
function again and print the new contents of the database to the console.
TEST
Make sure that the server and client are running.
Press the
GET Single Item
button on Step 11. That label should populate.Change the text in the input field while keeping the same ID# in the input field of Step 12 so that it is something different than what you had.
Press the
Update Item
button.You should see the following response in the console. A label will appear in Step 11 containing the new data, and the current contents of the database should also print to the console after the update runs:
Go ahead and refresh the page. You don't need to turn off the Client or Server.
Press the
GET Single Item
button on Step 11. The label should populate again.You should see that the newly updated data in the label and console message:
We might also suggest that you check in Postgres to be sure that you have successfully added the data to the database.
Getting Fancy
While we're here, let's mess around about with the stuff we're getting from the database. Go to your index.html
file and look for Step 12. We're going to add a little bit to one of the input fields:
You've used listeners in the past, but this time instead of adding one in JavaScript, we're using the HTML attribute onkeyup
. This listener looks for a key on the keyboard being released; in other words, after you press the key down, it waits until a key is released then does whatever you tell it to.
We're adding the listener to the updateNumber
input field, so anytime a key is released while that field is focus (i.e. we're typing something in the field), the function attached to the listener will run. this
is passed as a parameter into our function. Here, this
refers to the input field itself, which will allow us to easily access the value of the field in out function.
If you look at the button, you can see another one of these listener attributes onclick
. That one allows us to make the button do something without having to make it a submit
type. In fact, all of the buttons on this page have that listener attached.
showCurrentData()
The goal of this listener is to automatically search our database for an id
that matches what we've entered into the updateNumber
field, then populate the updateValue
filed with the data that is returned. Add this code to the bottom of your 03-auth-test.js
file:
Analysis
e
is the default variable name for an Event Listener. Here,e
represents the input fieldupdateNumber
, which was passed as a parameter usingthis
on the HTML page.We pass the value of the input field supplied by the user directly into the URL with a template literal. Because
e
is already defined as the input field, we don't need to use a function to get another reference to it.We call the DOM element we want to modify and set it to a variable to be accessed later.
If no item in the database matches the
id
we've supplied, the response comes back undefined. A blank return statement tells the program not to return anything and just to move on. Remember that not only does theid
have to match what's in the database, butuser.id
also has to match theowner
property, signifying that the current user is the one who entered it.We could use
innerHTML
to set the value, but that method doesn't work with<input>
elements. Instead, we usevalue
to insert our data into the field.
Again, this function will not run until a key is lifted after being pressed.
Testing
Your client and server should still be running; if not, restart them.
Refresh your browser, then type a number into the first field in step 12.
If nothing in the database matches the
id
you've entered, you should see something like this:If a match is found, you should see something like this:
You can then change the text however you'd like. Click
Update Item
and refresh your database to make sure it worked.
This process allows you to see what's in the database before you replace it. Most users, if not all, won't have direct access to the database, so this is one way that can show them what's already saved for a particular item.
Last updated