axios 我无法在Nextjs中使用SWR获取数据

cbjzeqam  于 2023-08-04  发布在  iOS
关注(0)|答案(1)|浏览(136)

我第一次尝试在Next.js中使用SWR。我正在使用Axios进行HTTP调用。Axios调用受保护的API路由,我使用拦截器处理。这里是我使用SWR的地方之一,但当我从SWR得到console.log(data)时,我得到了undefined;

const getProfile = async()=>{
    try {
       
        const res = await axiosPrivate.get<axiosGetCompanyProfile>(`/getcompany`, {withCredentials: true, headers: {Authorization: `${state?.in_memory}`}});
        console.log(res.data, 'mmmmnn')

        return res.data;
    } catch (error) {
        return error
    }
    
}

const { data, error, isLoading } = useSWR(`${getcompany`, getProfile)

字符串
我做错了什么?

bgtovc5b

bgtovc5b1#

有一个语法错误:

const getProfile = async () => {
    try {
        const res = await axiosPrivate.get<axiosGetCompanyProfile>(`/getcompany`, {
            withCredentials: true,
            headers: { Authorization: `${state?.in_memory}` },
        });
        return res.data;
    } catch (error) {
        throw error;
    }
};

const { data, error, isValidating } = useSWR('/getcompany', getProfile);

字符串

useSWR的第一个参数应该是一个字符串,作为标识数据的键(通常是API端点)。第二个参数是fetcher函数,在本例中是getProfile。

相关问题