无法读取NextJS服务器中未定义的属性'indexOf'

时间:2020-05-17 13:23:56

标签: reactjs next.js apollo apollo-client next

我的下一个JS网站运行良好,有一天,当我尝试从此处https://www.apollographql.com/docs/link/links/rest/#gatsby-focus-wrapper安装Apollo Rest时,我开始遇到很多错误。 我放弃了packages.json中的所有更改,删除了packages-lock.json,node_modules以及灰尘文件和文件夹。但是,请继续获取下一个错误:

GraphQL error occurred [getDataFromTree] TypeError: Cannot read property 'indexOf' of undefined
    at addBasePath (F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\index.js:2966:15)
    at F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\index.js:2464:40
    at Link.formatUrls (F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\index.js:2388:18)
    at Link.render (F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\index.js:2588:14)
    at processChild (F:\Projects\breadcrumps\showcase\node_modules\react-dom\cjs\react-dom-server.node.development.js:3134:18)
    at resolve (F:\Projects\breadcrumps\showcase\node_modules\react-dom\cjs\react-dom-server.node.development.js:2960:5)
    at ReactDOMServerRenderer.render (F:\Projects\breadcrumps\showcase\node_modules\react-dom\cjs\react-dom-server.node.development.js:3435:22)
    at ReactDOMServerRenderer.read (F:\Projects\breadcrumps\showcase\node_modules\react-dom\cjs\react-dom-server.node.development.js:3373:29)
    at renderToStaticMarkup (F:\Projects\breadcrumps\showcase\node_modules\react-dom\cjs\react-dom-server.node.development.js:4004:27)
    at process (F:\Projects\breadcrumps\showcase\node_modules\@apollo\react-ssr\lib\react-ssr.cjs.js:38:16)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
TypeError: Cannot read property 'indexOf' of undefined
    at addBasePath (F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\index.js:2966:15)
    at F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\index.js:2464:40
    at Link.formatUrls (F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\index.js:2388:18)
    at Link.render (F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\index.js:2588:14)
    at processChild (F:\Projects\breadcrumps\showcase\node_modules\react-dom\cjs\react-dom-server.node.development.js:3134:18)
    at resolve (F:\Projects\breadcrumps\showcase\node_modules\react-dom\cjs\react-dom-server.node.development.js:2960:5)
    at ReactDOMServerRenderer.render (F:\Projects\breadcrumps\showcase\node_modules\react-dom\cjs\react-dom-server.node.development.js:3435:22)
    at ReactDOMServerRenderer.read (F:\Projects\breadcrumps\showcase\node_modules\react-dom\cjs\react-dom-server.node.development.js:3373:29)
    at renderToString (F:\Projects\breadcrumps\showcase\node_modules\react-dom\cjs\react-dom-server.node.development.js:3988:27)
    at render (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\render.js:83:16)
    at renderPage (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\render.js:415:16)
    at Object.ctx.renderPage (F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\_document.js:905:30)
    at Function.getInitialProps (F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\_document.js:247:19)
    at Function.getInitialProps (F:\Projects\breadcrumps\showcase\dist\functions\next\server\static\development\pages\_document.js:916:85)
    at Object.loadGetInitialProps (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\lib\utils.js:59:29)
    at Object.renderToHTML (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\render.js:419:36)
Error: "CustomDocument.getInitialProps()" should resolve to an object. But found "undefined" instead.
    at Object.loadGetInitialProps (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\lib\utils.js:65:15)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Object.renderToHTML (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\render.js:419:22)
    at async DevServer.renderToHTMLWithComponents (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\next-server.js:654:26)
    at async DevServer.renderToHTML (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\next-server.js:801:28)
    at async DevServer.renderToHTML (F:\Projects\breadcrumps\showcase\node_modules\next\dist\server\next-dev-server.js:22:539)
    at async DevServer.render (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\next-server.js:546:22)
    at async Object.fn (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\next-server.js:396:17)
    at async Router.execute (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\router.js:134:32)
    at async DevServer.run (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\next-server.js:511:29)
    at async DevServer.handleRequest (F:\Projects\breadcrumps\showcase\node_modules\next\dist\next-server\server\next-server.js:147:20)

任何解决这个烦人问题的提示,我将不胜感激。

3 个答案:

答案 0 :(得分:9)

我发现问题出在哪里,我的网站链接之一获得了不确定的价值。 我正在使用“下一个/链接”组件。 但是获取传递给该链接的未定义URL的所有这三种异常是不合逻辑的。我认为NextJS开发团队应该更多地回顾他们如何记录异常。即使我为该组件传递了undefined,也应该对此进行检查,而不是整个网站都应该关闭。

答案 1 :(得分:4)

这可能不适用于您安装的Apollo,但由于我在react-router-dom中使用了Link语法,因此出现了相同的错误:

 <Link to="/home">

将其更改为NextJs语法已为我修复:

  <Link href="/home">

NextJs文档在这里: https://nextjs.org/learn/basics/navigate-between-pages/link-component

答案 2 :(得分:0)

对于搜索此错误的其他任何人,我在将 Link 作为道具传递给 Material-UI ListItem 时得到了一个,该道具以前曾用于在组件道具中传递 react-router-dom Link,如下所示:

<ListItem
    key={page.title}
    activeClassName={classes.activeListItem}
    className={classes.listItem}
    to={page.href}
    component={Link}
>

并且可以更改为:

<Link href={page.href}>
    <ListItem
        key={page.title}
        activeClassName={classes.activeListItem}
        className={classes.listItem}
    >
</Link>