Rashon
Rashon

Reputation: 75

Passing request parameters on an API fetch Request

How do I pass down parameters to a fetch request? I have this api call to fetch the logged in users username. How do I pass the returned results to the other fetch request route as it's request parameter?

//Gets logged in user's username

async function getProfile(){
  try {
    const response = await fetch(`${SUMMIT_API}/users/myprofile`,{
      method: 'GET',  
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${myToken}`
      },
    })
    const data = await response.json();
    console.log(data)
    return data.profile
  } catch (error) {
    console.log('Oops Something Went Wrong! Could not get that user profile.');
  }


} 

Results from above fetch: enter image description here

//Request parameters is the logged in user's username in the route retrieved from above fetch request


async function userChannel(){
  try {
    const response = await fetch(`${SUMMIT_API}/users/myprofile/**${username}**/channel`,{
      method: 'GET',  
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${myToken}`
      }
    })
    const data = await response.json();
    console.log(data)
    return data.profile;
  } catch (error) {
    console.log('Oops Something Went Wrong! Could not render userChannel');
  }


}

How do I get the information from first request passed to the second?

Upvotes: 0

Views: 3040

Answers (1)

apsillers
apsillers

Reputation: 115910

Since the information you want seems to be supplied by your async function getProfile, it seems like you simply need to await getProfile() and pull the necessary information out:

var profile = await getProfile();
var username = profile[0].username;

Upvotes: 1

Related Questions