-
Notifications
You must be signed in to change notification settings - Fork 1
π½ UserData (API)
File: Meesterproef-SNSimulation/js/UserData.js
The UserData class handles fetching and managing user data and post data. It pre-fetches data from APIs and provides methods to retrieve user and post data as needed. The class retrieves both fake and real data from APIs.
-
constructor(): Initializes a new instance of theUserDataclass, pre-fetching user and post data. -
fetchUserData(count): Fetches random user data from an API.-
Parameters:
-
count(number): The number of users to fetch.
-
- Returns: Promise - Resolves when the data has been fetched and processed.
- View Code
-
Parameters:
-
get(count): Retrieves a specified number of pre-fetched users. If insufficient data is available, it fetches more.-
Parameters:
-
count(number): The number of users to retrieve.
-
- Returns: Array - An array of user data objects.
- View Code
-
Parameters:
-
getPosts(count): Retrieves a specified number of random posts from the pre-fetched post data.-
Parameters:
-
count(number): The number of posts to retrieve.
-
- Returns: Array - An array of post data objects.
- View Code
-
Parameters:
-
fetchRealPostData(): Fetches real post data from an RSS feed, converting the XML data to JSON format.- Returns: Promise - Resolves when the data has been fetched and processed.
- View Code
This guide explains how to use an asynchronous function to fetch user data from the randomuser.me API and process the fetched data. The function retrieves a specified number of user profiles, extracts the necessary details, and stores them in an array.
Here is the complete code snippet with detailed comments:
async fetchUserData(count) {
// Fetch user data from the randomuser.me API
const response = await fetch(`https://randomuser.me/api/?results=${count}`);
// Parse the JSON response
const data = await response.json();
// Iterate over each person in the results array
data.results.forEach((person) => {
// Create an object with the person's image URL and full name
let personData = {
image: person.picture.large, // User's large picture URL
username: person.name.first + " " + person.name.last, // User's full name
};
// Add the person's data to the preFetchedData array
this.preFetchedData.push(personData);
});
}-
async fetchUserData(count): An asynchronous function namedfetchUserDatathat takes a single parametercount. Theasynckeyword allows the function to useawaitinside it.
-
const response = await fetch(...): Uses thefetchAPI to make an HTTP GET request tohttps://randomuser.me/api/?results=${count}. Theawaitkeyword pauses the function execution until the promise returned byfetchresolves. The number of results is determined by thecountparameter.
-
const data = await response.json();: The response fromfetchis aResponseobject. Thejsonmethod is called on this object to parse the response body as JSON.awaitpauses execution until the promise resolves with the parsed JSON data.
-
data.results.forEach((person) => { ... });: Thedataobject contains aresultsarray with user data. TheforEachmethod iterates over eachpersonin this array.
Inside the forEach loop, a personData object is created with two properties:
-
image: The URL to the user's large picture. -
username: The user's first and last name concatenated with a space.
This personData object is then pushed to the preFetchedData array of the class or object.
-
π Debrief
-
β οΈ Problem definition -
π§βπ¨ Design
-
π§βπ» Solution & Code
- ποΈ App (main file)
- π€ Simulation
- π΅ Node
- π Person
- π° Post
- γ°οΈ Edge
βοΈ Cursor- π½ userData
- π WebData
- π Import & Export (FileHandler)
- Onboarding
-
π€ Recommendations