为什么我的 useQuery 钩子之一没有返回未定义,而另一个则返回未定义?

发布于 2025-01-17 17:19:04 字数 1110 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖微凉心微凉 2025-01-24 17:19:04

我认为困惑在于你的第一个返回被称为数据。巧合的是,Axios 请求返回一个具有名为 data 的属性的对象,您已成功解构该对象。它们没有名为 WinnersData 的属性,因此 const {winnersData} = wait axios.get('http://localhost/api/choose-winners', {headers}); 未定义。

编辑:

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
    // Remember, there is no winnersData on the object Axios is returning

    const {data} = await axios.get('http://localhost/api/choose-winners', {headers});
    return data
    // There is a data property for you to destructure (as there is on every successful Axios return), so we'll return that!

}

const data = useQuery('uploads', fetchUploads)
const winnersData = useQuery('winners', fetchWinners)
// I'm not sure what the shape of the data returned by these functions is, so I took out the destructuring. Remember, you can't destructure a property that isn't there.

console.log(data); // returns correct data
console.log(winnersData); // returns undefined

return(...);

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:

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
    // Remember, there is no winnersData on the object Axios is returning

    const {data} = await axios.get('http://localhost/api/choose-winners', {headers});
    return data
    // There is a data property for you to destructure (as there is on every successful Axios return), so we'll return that!

}

const data = useQuery('uploads', fetchUploads)
const winnersData = useQuery('winners', fetchWinners)
// I'm not sure what the shape of the data returned by these functions is, so I took out the destructuring. Remember, you can't destructure a property that isn't there.

console.log(data); // returns correct data
console.log(winnersData); // returns undefined

return(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文