next-redux-wrapper TypeError: nextCallback is not a function error in wrapper.getServerSideProps

Posted

技术标签:

【中文标题】next-redux-wrapper TypeError: nextCallback is not a function error in wrapper.getServerSideProps【英文标题】: 【发布时间】:2021-10-08 19:50:04 【问题描述】:

我在尝试导出 wrapper.getServerSideProps 函数时收到 TypeError: nextCallback is not a function 错误。

我的代码

import App from '../components/App'
import wrapper from '../redux/Store';
import getRooms from '../redux/actions/RoomAction'

export default function Index() 
  return (
    <App />
  )


export const getServerSideProps = wrapper.getServerSideProps(async (req, store) => 
     await store.dispatch(getRooms(req))
)

我在导出 getServerSideProps 时遇到错误。

输出

Zia-Sultan:bookit Raymond$ npm run dev

> bookit@0.1.0 dev /Users/Raymond Tucker/projects/next/BOOKIT/bookit
> next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
event - compiled successfully
event - build page: /
wait  - compiling...
event - compiled successfully
TypeError: nextCallback is not a function
    at /Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:146:46
    at step (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:57:23)
    at Object.next (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:38:53)
    at /Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:32:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:28:12)
    at makeProps (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:135:16)
    at /Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:186:46
    at step (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:57:23)
    at Object.next (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:38:53)
Warning: Invalid DOM property `crossorigin`. Did you mean `crossOrigin`?
    at link
    at head
    at Head (webpack-internal:///./node_modules/next/dist/pages/_document.js:252:5)
    at html
    at Html (webpack-internal:///./node_modules/next/dist/pages/_document.js:241:29)
    at MyDocument (webpack-internal:///./pages/_document.js:17:1)
(node:5114) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'dispatch' of undefined
    at eval (webpack-internal:///./pages/index.js:27:15)
    at /Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:143:52
    at step (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:57:23)
    at Object.next (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:38:53)
    at /Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:32:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:28:12)
    at makeProps (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:135:16)
    at /Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:186:46
    at step (/Users/Raymond Tucker/projects/next/BOOKIT/bookit/node_modules/next-redux-wrapper/lib/index.js:57:23)
(node:5114) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5114) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

【参考方案1】:

传递给wrapper.getServerSideProps 的函数的签名已在next-redux-wrapper v7.x 中更改。

替换以下内容。

export const getServerSideProps = wrapper.getServerSideProps(async (req, store) => 
     // Code here
)

有正确的签名。

export const getServerSideProps = wrapper.getServerSideProps((store) => async ( req ) => 
     // Code here
)

【讨论】:

以上是关于next-redux-wrapper TypeError: nextCallback is not a function error in wrapper.getServerSideProps的主要内容,如果未能解决你的问题,请参考以下文章

next-redux-wrapper 中的空状态

将 getStaticProps 与 next-redux-wrapper 一起使用

使用 next-redux-wrapper 将 Google Analytics 添加到 Nextjs 应用程序

next-redux-wrapper 服务器不与客户端同步?

关于未找到状态且不可分配给类型的 next-redux-wrapper 的 Typescript 警告

Next.js:在 getServerSideProps 中使用带有 TypeScript 的 next-redux-wrapper 调用 Thunks?