【发布时间】:2021-09-24 13:51:35
【问题描述】:
如何将参数传递给获取请求?我有这个 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');
}
}
如何从第一个请求传递到第二个请求的信息?
【问题讨论】:
-
你只是想在
userChannel开头做var profile = await getProfile(); var username = profile[0].username;吗? -
不确定,我会尝试,我只想将返回的用户名作为参数传递给另一个 fetch。如何从结果中提取它?
-
这正是我所需要的,谢谢!!
标签: javascript parameter-passing fetch-api