【问题标题】:TypeError: resolver is not a function in `next-connect`TypeError:解析器不是`next-connect`中的函数
【发布时间】:2021-11-17 12:00:26
【问题描述】:

我有一个项目使用next-connect & twitter-api-v2

我有一个 handler 调用 2 个 get 路由,例如:

export default handler()
  .get('/twitter/generate-auth-link', generateAuthLink)
  .get('/twitter/get-verifier-token', getVerifierToken)

handler 如下所示:

import { NextApiResponse } from 'next'
import cookieSession from 'cookie-session'
import nc from 'next-connect'
import { ironSession } from 'next-iron-session'
import { error } from 'next/dist/build/output/log'
import { NextIronRequest } from '../types/index'

const COOKIE_SECRET = process.env.COOKIE_SECRET
const SESSION_SECRET = process.env.SESSION_SECRET

const IS_PRODUCTION = process.env.NODE_ENV !== 'development'

/**
 * Create an API route handler with next-connect and all the necessary middlewares
 *
 * @example
 * ```ts
 * export default handler().get((req, res) => { ... })
 * ```
 */
function handler() {
  if (!COOKIE_SECRET || !SESSION_SECRET)
    throw new Error(
      `Please add COOKIE_SECRET & SESSION_SECRET to your .env.local file!`
    )

  return nc<NextIronRequest, NextApiResponse>({
    onError: (err, _, res) => {
      error(err)
      res.status(500).end(err.toString())
    },
  })
    .use(
      cookieSession({
        name: 'session',
        keys: [COOKIE_SECRET],
        maxAge: 24 * 60 * 60 * 1000 * 30,
        secure: IS_PRODUCTION && !process.env.INSECURE_AUTH,
        signed: IS_PRODUCTION && !process.env.INSECURE_AUTH,
      })
    )
    .use(
      ironSession({
        cookieName: 'mysite-session',
        password: SESSION_SECRET,
        // if your localhost is served on http:// then disable the secure flag
        cookieOptions: {
          secure: IS_PRODUCTION,
        },
      })
    )
}

export default handler

我认为我在做next-connect 是对的,但是当我尝试在客户端中执行简单的fetch 时为什么会出错:

const res = await fetch('/api/twitter/generate-auth-link');
console.log({res})

后端报错:

error - TypeError: resolver is not a function
    at Object.apiResolver (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/api-utils.js:101:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async DevServer.handleApiRequest (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/next-server.js:770:9)
    at async Object.fn (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/next-server.js:661:37)
    at async Router.execute (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/router.js:205:32)
    at async DevServer.run (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/next-server.js:841:29)
    at async DevServer.run (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/dev/next-dev-server.js:355:20)
    at async DevServer.handleRequest (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/next-server.js:292:20) {
  page: '/api/twitter/generate-auth-link'
}

我已经做了一个完整的repro→https://github.com/deadcoder0904/twitter-api-v2-3-legged-login-using-next-connect

我想要做的就是使用twitter-api-v2 执行 3-legged Oauth 步骤。步骤在https://github.com/PLhery/node-twitter-api-v2/blob/master/doc/auth.md上给出

我该如何解决这个问题?

【问题讨论】:

    标签: node.js next.js next-connect


    【解决方案1】:

    我从pages/api/twitter/ 中删除了index.ts 文件,并在pages/api/twitter/generate-auth-linkpages/api/twitter/get-verifier-token 中使用了默认导出。

    这就解决了!

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2021-11-12
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 2023-02-21
      • 2020-05-28
      • 2016-12-28
      • 2020-09-18
      相关资源
      最近更新 更多