在 API 获取请求上传递请求参数

Passing request parameters on an API fetch Request

如何将参数传递给提取请求?我有这个 api 调用来获取登录用户的用户名。如何将返回的结果作为请求参数传递给其他获取请求路由?

//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.');
  }


} 

上面获取的结果:

//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');
  }


}

如何从传递给第二个请求的第一个请求中获取信息?

由于您想要的信息似乎是由您的 async function getProfile 提供的,看来您只需要 await getProfile() 并提取必要的信息即可:

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