我如何破坏质查询异步返回变量,以使其中一个不是log dog nog undefined?

发布于 2025-01-17 21:28:08 字数 1560 浏览 0 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

悟红尘 2025-01-24 21:28:08

看起来USEQUERY总是返回一个名为数据的属性,因此您只需要重命名它(对于Axios返回而言相同)。而且不要忘记等待Axio的电话。

const { data } = await axios.get('http://localhost/api/choose-winners', {headers});
return data;
...
const { data } = useQuery('uploads', fetchUploads);
const { data: winnersData } = useQuery('winners', fetchWinners);
console.log('winnersData', winnersData);

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.

const { data } = await axios.get('http://localhost/api/choose-winners', {headers});
return data;
...
const { data } = useQuery('uploads', fetchUploads);
const { data: winnersData } = useQuery('winners', fetchWinners);
console.log('winnersData', winnersData);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文