Error: 无法读取未定义的属性(读取 ‘Symbol(__APOLLO_CONTEXT__)’)

huangapple go评论51阅读模式
英文:

Error: Cannot read properties of undefined (reading 'Symbol(__APOLLO_CONTEXT__)')

问题

I understand you want a translation of the code part only. Here's the translated code:

我有一个在Next.js项目中的问题

我正在尝试在我的Next.js项目中使用Apollo客户端获取数据之前有效的代码现在不起作用了我尝试完全删除node_modules文件夹并重新安装我尝试删除_next文件夹并再次运行它我尝试更新npm包我尝试了所有我在论坛上找到的方法但它不起作用

这个错误消息

未处理的运行时错误
错误无法读取未定义的属性读取'symbol(__APOLLO_CONTEXT__)'

这是错误消息的来源/详细信息
来源

contextKey
node_modules\@apollo\client\react\context\context.cjs (27:49)
getApolloContext
node_modules\@apollo\client\react\hooks\hooks.cjs (30:45)
useApolloClient
node_modules\@apollo\client\react\hooks\hooks.cjs (86:28)
app\[blog]\page.jsx (32:44) @ result

30 | export default function Hakkimda({ params, launches }) {
31 | //console.log("launches", launches);
> 32 | const { loading, error, data } = useQuery(result, client);
| ^

33 |
34 | if (loading) return <p>Loading...</p>;
35 | if (error) return <p>Error: {error.message}</p>;
调用堆栈
类型
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (1447:17)
attemptResolveElement
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (1759:20)
resolveModelToJSON
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (1249:13)
stringify
<anonymous>
stringify
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (181:13)
processModelChunk
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (2062:25)
retryTask
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (2109:6)
performWork
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (1544:13)
listOnTimeout
node:internal/timers (569:17)
process.processTimers
node:internal/timers (512:7)

这个问题发生在我尝试使用Apollo获取数据时
这是代码
apolloclient.js代码

import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";

const client = new ApolloClient({
   uri: "https://api.mocki.io/v2/c4d7a195/graphql",
   cache: new InMemoryCache(),
});

export default client;

我的下一个页面代码

import { gql, useQuery } from "@apollo/client";
import client from "../lib/apolloClient";

const GET_POSTS = gql`
   {
     users {
       ID
       e-mail
       name
     }
   }
`;
const result = await client.query({
   query: gql`
{
   users {
     ID
     e-mail
     name
   }
}
`,
});


export default function About Me({ params, launches }) {
   // console.log("launches", launches);
   const { loading, error, data } = useQuery(result, client);

   if (loading) return <p>Loading...</p>;
   if (error) return <p>Error: {error.message}</p>;

   return (
     <main className="container min-h-screen px-8 mx-auto max-w-7xl">
       <h1 className="mb-6 text-2xl capitalize">Incoming Parameter: {}</h1>
       <div></div>
     </main>
   );
}

export async function getStaticProps() {
   return {
     props: {
       launches: [],
     },
   };
}

package.json:

{
   "name": "salihonderfrontend",
   "version": "0.1.0",
   "private": true,
   "scripts": {
     "dev": "next dev",
     "build": "next build",
     "start": "next start",
     "lint": "next lint",
     "cache-clean": "npm cache clean --force && yarn cache clean"
   },
   "dependencies": {
     "@apollo/client": "^3.7.14",
     "@fortawesome/free-brands-svg-icons": "^6.4.0",
     "@fortawesome/free-solid-svg-icons": "^6.4.0",
     "@fortawesome/react-fontawesome": "^0.2.0",
     "@headlessui/react": "^1.7.14",
     "@heroicons/react": "^2.0.17",
     "autoprefixer": "^10.4.14",
     "eslint": "^8.40.0",
     "eslint-config-next": "^13.4.1",
     "graphql": "^16.6.0",
     "next": "^13.4.1",
     "postcss": "^8.4.23",
     "react": "^18.2.0",
     "react-dom": "^18.2.0",
     "tailwindcss": "^3.3.2"
   },
   "browser": {
     "fs": false,
     "os": false,
     "path": false
   }
}

next.config.js:

/** @type { import('next').NextConfig} */
const nextConfig = {
   //reactStrictMode: true,
   //swcMinify: true,
   experimental: {
     appDir: true,
   },
   webpack: (config) => {
     config.experiments = config.experiments || {};
     config.experiments.topLevelAwait = true;
     return config;
   },
};

module.exports = nextConfig;

Please note that the code includes the same error message and comments as in the original text.

英文:

i have issue a next.js project.

I am trying to get data with apollo client in my next.js project. Codes that worked before do not work now. I tried deleting the node_modules folder completely and installing. I tried deleting the _next folder and running it again. I tried updating the npm packages. I tried all the methods I found on the forums. However, it doesn't work.

This error message:

Unhandled Runtime Error
Error: Cannot read properties of undefined (reading &#39;Symbol(__APOLLO_CONTEXT__)&#39;)

This is error message source/details:
Source

contextKey
node_modules\@apollo\client\react\context\context.cjs (27:49)
getApolloContext
node_modules\@apollo\client\react\hooks\hooks.cjs (30:45)
useApolloClient
node_modules\@apollo\client\react\hooks\hooks.cjs (86:28)
app\[blog]\page.jsx (32:44) @ result
30 | export default function Hakkimda({ params, launches }) {
31 | //console.log(&quot;launches&quot;, launches);
&gt; 32 | const { loading, error, data } = useQuery(result, client);
|                                          ^
33 | 
34 | if (loading) return &lt;p&gt;Loading...&lt;/p&gt;;
35 | if (error) return &lt;p&gt;Error: {error.message}&lt;/p&gt;;
Call Stack
type
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (1447:17)
attemptResolveElement
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (1759:20)
resolveModelToJSON
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (1249:13)
stringify
&lt;anonymous&gt;
stringify
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (181:13)
processModelChunk
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (2062:25)
retryTask
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (2109:6)
performWork
node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (1544:13)
listOnTimeout
node:internal/timers (569:17)
process.processTimers
node:internal/timers (512:7)

this problem happens when i try to get data with apollo.
this is the codes:
apolloclient.js codes:

import { ApolloClient, ApolloProvider, InMemoryCache } from &quot;@apollo/client&quot;;
const client = new ApolloClient({
uri: &quot;https://api.mocki.io/v2/c4d7a195/graphql&quot;,
cache: new InMemoryCache(),
});
export default client;

My next page codes:

import { gql, useQuery } from &quot;@apollo/client&quot;;
import client from &quot;../lib/apolloClient&quot;;
const GET_POSTS = gql`
{
users {
ID
e-mail
name
}
}
`;
const result = await client.query({
query: gql`
{
users {
ID
e-mail
name
}
}
`,
});
export default function About Me({ params, launches }) {
// console.log(&quot;launches&quot;, launches);
const { loading, error, data } = useQuery(result, client);
if (loading) return &lt;p&gt;Loading...&lt;/p&gt;;
if (error) return &lt;p&gt;Error: {error.message}&lt;/p&gt;;
return (
&lt;main className=&quot;container min-h-screen px-8 mx-auto max-w-7xl&quot;&gt;
&lt;h1 className=&quot;mb-6 text-2xl capitalize&quot;&gt;Incoming Parameter: {}&lt;/h1&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/main&gt;
);
}
export async function getStaticProps() {
return {
props: {
launches: [],
},
};
}

package json:

{
&quot;name&quot;: &quot;salihonderfrontend&quot;,
&quot;version&quot;: &quot;0.1.0&quot;,
&quot;private&quot;: true,
&quot;scripts&quot;: {
&quot;dev&quot;: &quot;next dev&quot;,
&quot;build&quot;: &quot;next build&quot;,
&quot;start&quot;: &quot;next start&quot;,
&quot;lint&quot;: &quot;next lint&quot;,
&quot;cache-clean&quot;: &quot;npm cache clean --force &amp;&amp; yarn cache clean&quot;
},
&quot;dependencies&quot;: {
&quot;@apollo/client&quot;: &quot;^3.7.14&quot;,
&quot;@fortawesome/free-brands-svg-icons&quot;: &quot;^6.4.0&quot;,
&quot;@fortawesome/free-solid-svg-icons&quot;: &quot;^6.4.0&quot;,
&quot;@fortawesome/react-fontawesome&quot;: &quot;^0.2.0&quot;,
&quot;@headlessui/react&quot;: &quot;^1.7.14&quot;,
&quot;@heroicons/react&quot;: &quot;^2.0.17&quot;,
&quot;autoprefixer&quot;: &quot;^10.4.14&quot;,
&quot;eslint&quot;: &quot;^8.40.0&quot;,
&quot;eslint-config-next&quot;: &quot;^13.4.1&quot;,
&quot;graphql&quot;: &quot;^16.6.0&quot;,
&quot;next&quot;: &quot;^13.4.1&quot;,
&quot;postcss&quot;: &quot;^8.4.23&quot;,
&quot;react&quot;: &quot;^18.2.0&quot;,
&quot;react-dom&quot;: &quot;^18.2.0&quot;,
&quot;tailwindcss&quot;: &quot;^3.3.2&quot;
},
&quot;browser&quot;: {
&quot;fs&quot;: false,
&quot;os&quot;: false,
&quot;path&quot;: false
}
}

next.config.js:

/** @type { import(&#39;next&#39;).NextConfig} */
const nextConfig = {
//reactStrictMode: true,
//swcMinify: true,
experimental: {
appDir: true,
},
webpack: (config) =&gt; {
config.experiments = config.experiments || {};
config.experiments.topLevelAwait = true;
return config;
},
};
module.exports = nextConfig;

I think I tried everything. ChatGPT couldn't find a solution Error: 无法读取未定义的属性(读取 ‘Symbol(__APOLLO_CONTEXT__)’)

答案1

得分: 7

I will assume that this is in the Next.js App Router.

You can import the Apollo Client Provider component and the Apollo Client hooks only in files that are part of a React Client Component.

That means that the file that contains the import of Apollo Client hooks or Provider has to be either marked "use client", or has to be imported by a file that is marked "use client" (or a file that imports a file and so on).

In your case, you seem to be doing that from a React Server Component, which causes that error.

英文:

I will assume that this is in the Next.js App Router.

You can import the Apollo Client Provider component and the Apollo Client hooks only in files that are part of a React Client Component.

That means that the file that contains the import of Apollo Client hooks or Provider has to be either marked &quot;use client&quot;, or has to be imported by a file that is marked &quot;use client&quot; (or a file that imports a file and so on).

In your case, you seem to be doing that from a React Server Component, which causes that error.

答案2

得分: 1

你需要创建一个提供者组件,并将Next.js的根布局包装在该提供者内。注意:你创建的提供者需要在文件顶部指示use client

import { ApolloProvider } from "@apollo/client";
import { client } from "./xxxxxx";

export function ApolloClientProvider({ children }: { children: React.ReactNode }) {
  return (
    <ApolloProvider client={client}>
      {children}
    </ApolloProvider>
  )
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body className={inter.className}>
        <ApolloClientProvider >
          {children}
        </ApolloClientProvider>
      </body>
    </html>
  );
}

请注意,这是提供者组件的示例,你需要根据你的需求和文件结构进行适当的调整。

英文:

You need to create a provider component and wrap the next.js root layout with the provider, NOTE: the provider you create needs to indicate use client at the top of the file.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import { ApolloProvider } from &quot;@apollo/client&quot;;
import { client } from &quot;./xxxxxx&quot;;
export function ApolloClientProvider({ children }: { children: React.ReactNode }) {
return (
&lt;ApolloProvider client={client}&gt;
{children}
&lt;/ApolloProvider&gt;
)
}
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
&lt;html lang=&quot;en&quot;&gt;
&lt;body className={inter.className}&gt;
&lt;ApolloClientProvider &gt;
{children}
&lt;/ApolloClientProvider&gt;
&lt;/body&gt;
&lt;/html&gt;
);
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月6日 18:40:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定