为什么我的 useQuery 钩子之一没有返回未定义,而另一个则返回未定义?
我正在使用 React Query,但我不确定为什么我的 useQuery 挂钩之一正在记录 undefined
而另一个则正在记录正确的数据。它们都是异步函数,并且实际上在做同样的事情。我已经尝试调试这个有一段时间了,但无济于事,我已经碰壁了。
我该如何修复它,以便 console.log(winnersData);
也像 console.log(data);
一样记录正确的数据?
注意:是的,在 Postman 上测试时,fetchUploads()
正确返回数据,因此我们可以排除没有返回任何内容的情况。
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} = await axios.get('http://localhost/api/choose-winners', {headers});
return winnersData
}
const { data } = useQuery('uploads', fetchUploads)
const { winnersData } = useQuery('winners', fetchWinners)
console.log(data); // returns correct data
console.log(winnersData); // returns undefined
return(...);
I'm using React Query and I'm not sure why one of my useQuery hooks is logging undefined
while the other one is logging the correct data. They're both async functions and are literally doing the same thing. I've been trying to debug this for a while but to no avail, I've hit a wall.
How can I fix it so that console.log(winnersData);
also logs the correct data just like console.log(data);
?
Note: Yes, fetchUploads()
is returning data correctly when testing on Postman so we can rule out that nothing's being returned.
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} = await axios.get('http://localhost/api/choose-winners', {headers});
return winnersData
}
const { data } = useQuery('uploads', fetchUploads)
const { winnersData } = useQuery('winners', fetchWinners)
console.log(data); // returns correct data
console.log(winnersData); // returns undefined
return(...);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为困惑在于你的第一个返回被称为数据。巧合的是,Axios 请求返回一个具有名为 data 的属性的对象,您已成功解构该对象。它们没有名为 WinnersData 的属性,因此 const {winnersData} = wait axios.get('http://localhost/api/choose-winners', {headers}); 未定义。
编辑:
I think the confusion revolves around your first return being called data. Coincidentally, Axios requests return an object with a property called data, which you are successfully destructuring. They don't have a property called winnersData, hence
const {winnersData} = await axios.get('http://localhost/api/choose-winners', {headers});
is undefined.Edit: