We'll be using this random user generator API to get information about user(s) and display that information on the webpage.
The API can be accessed by making a GET fetch request to this url: https://randomuser.me/api/
To gain a better understanding about the API and the data it returns, I recommend going to that URL in your web browser and looking at the data it gives you.
This is the same process that we need to recreate by using the fetch()
method in our JavaScript code.
- Make a new folder inside of
/repos
and open it with Visual Studio Code - Make the usual
index.html
file - Be sure to attach a link to a
script.js
file and defer its execution - Optionally make a
style.css
file and link to it inside of your html file - Feel free to add some HTML and CSS to your webpage to make it your own. Maybe create an
h1
tag, and/orp
tags... Have fun with it
- Create a function called
getUserData
. It doesn't require any parameters yet (we will refactor this function in a bit) - Use the
fetch()
method to send a request to this URL:https://randomuser.me/api/
- Consume the promise that it returns by chaining a
.then()
method onto yourfetch
. - Provide a callback function as a parameter to the
.then()
method. The callback function should have a parameter (we'll name itresponse
). The callback function's body should parse the response object's JSON data by invoking.json()
and return the result. - Handle the next promise by again chaining a
.then()
method onto the previous.then()
method. - Again, provide a callback function which takes one parameter (we'll call it data). This time the callback function's body should simply log out the
data
parameter. - Handle any potential errors by chaining a
.catch()
method onto the previous.then()
method. - Provide the
.catch()
with a callback function which takes one parameter (we'll call it err). The callback function's body shouldconsole.error()
theerr
parameter.
- Above or below this function you just made, simply invoke the function
getUserData()
. - Run and test your code. Open the browser console to see if the random user data shows up in your console. Work through any errors you may be getting.
Now that you're able to get a user's data, extract just the pieces of information that you want to display
- Create a new function called
displayUser
. This function will take a parameter (we'll call ituserData
). - Create temporary variables for the user's name, city, country, email, and large picture. Each variable will need to be assigned the appropriate value from the
userData
object. For example, the name will need to be a concatenation ofuserData.name.first
anduserData.name.last
. (You can include the title if you'd like, as well)
Create HTML elements to contain the information and add them to the DOM. You'll need to use DOM methods and properties like document.createElement()
, .append()
, .textContent
, etc...
- Create a div with a class of
userContainer
- Create an img with a
src
property that points to the large picture variable you created earlier, and analt
property that uses the name variable you created earlier. - Create separate paragraph tags for the user's name, city, country, and email. Be sure to set each of their
textContent
properties to the appropriate variables you made earlier. - Append the img and paragraphs to the
userContainer
div - Append the div onto the DOM wherever you'd like (Select an element first, or simply attach onto the
document.body
)
Lastly, you'll need to invoke this displayUser
function at the right time
- You can only show a user's data on the screen once you have a user's data. So, back in the
getUserData
function, inside the callback function of the second.then()
method, invoke the function:displayUser(data.results[0]);
Now all the pieces of our code should be tied together, and hopefully, you'll see the user's data on your webpage.