如何向下传递参数到fetch请求?我有这个api调用来获取登录用户的用户名。如何将返回的结果作为其请求参数传递给另一个fetch请求路由?
//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');
}
}如何从传递给第二个请求的第一个请求中获取信息?
发布于 2021-07-16 00:46:03
由于您需要的信息似乎是由您的async function getProfile提供的,因此您似乎只需要await getProfile()并提取出所需的信息:
var profile = await getProfile();
var username = profile[0].username;https://stackoverflow.com/questions/68397468
复制相似问题