我如何破坏质查询异步返回变量,以使其中一个不是log dog nog undefined?
Axios 对象没有 winnersData
属性,因此解构它会在原始 ocde 块中返回 undefined
。我需要解构来自两个 Axios 请求的数据,并且我稍后对它们的称呼需要不同。我只是不确定如何实现这一点并且已经挣扎了一段时间。我下面的尝试也失败了。
我怎样才能实现这个?
原始代码块:
async function fetchUploads(){
const headers = {
"Accept": 'application/json',
"Authorization": `Bearer ${authToken}`
};
const {data} = await axios.get('http://localhost/api/get-user-uploads-data', {headers});
return data;
}
async function fetchWinners(){
const headers = {
"Accept": 'application/json',
"Authorization": `Bearer ${authToken}`
};
const {winnersData} = axios.get('http://localhost/api/choose-winners', {headers});
return winnersData
}
const { data } = useQuery('uploads', fetchUploads)
const { winnersData } = useQuery('winners', fetchWinners)
console.log(data); // logs data correctly
console.log(winnersData); // logs undefined
return(....);
尝试修复 fetchWinners()
我已经尝试过,但失败了:
async function fetchWinners(){
const headers = {
"Accept": 'application/json',
"Authorization": `Bearer ${authToken}`
};
const { winnersData } = axios.get('http://localhost/api/choose-winners', {headers});
return { winnersData };
}
const { data } = useQuery('uploads', fetchUploads)
const { data: { winnersData } } = useQuery('winners', fetchWinners)
console.log(data.winnersData); // still logs undefined
The Axios object doesn't have a winnersData
property, so destructuring it returns undefined
in the original block of ocde. I need to destructure data from both Axios requests and what I call them later on needs to be different. I'm just not sure how to implement this and have been struggling for a while. My attempt below fails as well.
How can I implement this?
Original block of code:
async function fetchUploads(){
const headers = {
"Accept": 'application/json',
"Authorization": `Bearer ${authToken}`
};
const {data} = await axios.get('http://localhost/api/get-user-uploads-data', {headers});
return data;
}
async function fetchWinners(){
const headers = {
"Accept": 'application/json',
"Authorization": `Bearer ${authToken}`
};
const {winnersData} = axios.get('http://localhost/api/choose-winners', {headers});
return winnersData
}
const { data } = useQuery('uploads', fetchUploads)
const { winnersData } = useQuery('winners', fetchWinners)
console.log(data); // logs data correctly
console.log(winnersData); // logs undefined
return(....);
Attempted fix for fetchWinners()
I've tried but it failed:
async function fetchWinners(){
const headers = {
"Accept": 'application/json',
"Authorization": `Bearer ${authToken}`
};
const { winnersData } = axios.get('http://localhost/api/choose-winners', {headers});
return { winnersData };
}
const { data } = useQuery('uploads', fetchUploads)
const { data: { winnersData } } = useQuery('winners', fetchWinners)
console.log(data.winnersData); // still logs undefined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来USEQUERY总是返回一个名为数据的属性,因此您只需要重命名它(对于Axios返回而言相同)。而且不要忘记等待Axio的电话。
Looks like useQuery always returns a property called data, so you just need to rename it (same for the axios return). And don't forget an await for the axios call.