接下来的JS React-Query API调用使用USEQUERY()在客户端上工作,但是在服务器上的GetServersIdeProps中,等效的API调用不适

发布于 2025-01-18 17:34:33 字数 1602 浏览 1 评论 0原文

我正在尝试使用下一个JS getServersideProps中的React查询从后端服务器获取数据。这是获取数据的功能:

export const getGoogleAuthUrl = async () => {
  const res = await fetch(`${process.env.NEXT_PUBLIC_EXTERNAL_API_URL}/get-url`)
  return await res.json();
}

这是我的getserversideprops代码:

export async function getServerSideProps(context) {

  const queryClient = new QueryClient();
  await queryClient.prefetchQuery('getGoogleAuthUrl', getGoogleAuthUrl);

  // Pass data to the page via props
  return { 
    props: { 
      dehydratedState: dehydrate(queryClient)
    } 
  }
}

在我的控制台中,我会收到以下错误: fetcherror:http:// backend-server/api/get-url原因无效JSON响应主体:

如果我尝试记录脱水在JSON中的意外令牌P >在客户端中,通过将其作为道具传递,我在这样的状态数组中什么也没有得到:

{mutations: Array(0), queries: Array(0)}

现在,如果我在客户端使用useQuery进行相同的API调用,我会得到正确的响应,没有否错误。这是我在客户端称呼的方式:

export default function GetUrl({ dehydratedState }) {
  // The dehydrated state from the getServerSideProps being logged as empty because it 
  // encounters an error and returns nothing
  console.log(dehydratedState);

  // API call with react-query useQuery method

  const {
    isLoading,
    error,
    data
  } = useQuery('googleAuthUrl', getGoogleAuthUrl);


  if(isLoading) return <p>Loading...</p>
  if(error) return <p>Error: {error.message}</p>
  if(data) console.log(data); //Logs correct data to the console

  return (
    <>
      Get Url from server
    </>
  )
}

I am trying to fetch data from the backend server using React Query in Next JS getServerSideProps. Here is the function to get the data:

export const getGoogleAuthUrl = async () => {
  const res = await fetch(`${process.env.NEXT_PUBLIC_EXTERNAL_API_URL}/get-url`)
  return await res.json();
}

Here is my getServerSideProps code:

export async function getServerSideProps(context) {

  const queryClient = new QueryClient();
  await queryClient.prefetchQuery('getGoogleAuthUrl', getGoogleAuthUrl);

  // Pass data to the page via props
  return { 
    props: { 
      dehydratedState: dehydrate(queryClient)
    } 
  }
}

In my console, I get the following error:
FetchError: invalid json response body at http://backend-server/api/get-url reason: Unexpected token p in JSON at position 4

If I try to log the dehydratedState in the client by passing it as a prop, I get nothing in the state array like this:

{mutations: Array(0), queries: Array(0)}

Now if I make the same API call with useQuery on the client side, I get the correct response with no error. Here is how I call it on the client:

export default function GetUrl({ dehydratedState }) {
  // The dehydrated state from the getServerSideProps being logged as empty because it 
  // encounters an error and returns nothing
  console.log(dehydratedState);

  // API call with react-query useQuery method

  const {
    isLoading,
    error,
    data
  } = useQuery('googleAuthUrl', getGoogleAuthUrl);


  if(isLoading) return <p>Loading...</p>
  if(error) return <p>Error: {error.message}</p>
  if(data) console.log(data); //Logs correct data to the console

  return (
    <>
      Get Url from server
    </>
  )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

吃不饱 2025-01-25 17:34:33

您的问题可能与您尝试访问的 API 的配置方式有关。

最有可能的是,他们使用可疑的用户代理字符串或非住宅 IP 地址来阻止流量。许多第三方 API 这样做是为了防止系统滥用。谷歌对此尤其严格。

我要么尝试欺骗用户代理字符串来看看是否可以解决它,要么接受它作为由于 API 的安全限制而需要在客户端加载的数据。

Your problem is probably something to do with the way the API you are trying to access is configured.

Most likely, they are blocking traffic with suspicious user-agent strings, or perhaps with non-residential IP addresses. A lot of third-party APIs do this to prevent system abuse. Google in particular is quite strict with this.

I'd either try spoofing a user-agent string to see if you can work around it, or accept it as data that needs to be loaded client-side because of the API's security restrictions.

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