0

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?

3
  • 1
    Are you simply looking to do var profile = await getProfile(); var username = profile[0].username; at the beginning of userChannel? Commented Jul 15, 2021 at 16:28
  • Not sure, I will try that, I just want the returned username, to be passed in to the other fetch as it's parameter. How do I extract that out of the results? Commented Jul 15, 2021 at 16:31
  • That was what I needed, thank you!! Commented Jul 15, 2021 at 16:43

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.