首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在API获取请求上传递请求参数

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

Stack Overflow用户
提问于 2021-07-16 00:24:38
回答 1查看 82关注 0票数 0

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

代码语言:javascript
复制
//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.');
  }


} 

从上面获取的结果:

代码语言:javascript
复制
//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');
  }


}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-16 00:46:03

由于您需要的信息似乎是由您的async function getProfile提供的,因此您似乎只需要await getProfile()并提取出所需的信息:

代码语言:javascript
复制
var profile = await getProfile();
var username = profile[0].username;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68397468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档