hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.63k stars 65 forks source link

TypeError: Cannot read property 'routes' of undefined #144

Closed izecubz closed 3 years ago

izecubz commented 3 years ago

I've been getting this error more recently and it's been bugging me for a while, I've tried the following:

Here is the full error

C:\Users\**\Documents\GitHub\**\****\node_modules\next-connect\dist\index.cjs:1
TypeError: Cannot read property 'routes' of undefined
    at mount (C:\Users\**\Documents\GitHub\**\****\node_modules\next-connect\dist\index.cjs:6:27)
    at Array.map (<anonymous>)
    at Function.use (C:\Users\**\Documents\GitHub\**\****\node_modules\next-connect\dist\index.cjs:43:23)
    at Function.use (C:\Users\**\Documents\GitHub\**\****\node_modules\next-connect\dist\index.cjs:29:47)
    at Object../pages/api/user.js (C:\Users\**\Documents\GitHub\**\****\.next\server\pages_api_user_js-server_lib_passport_js-server_middleware_auth_js.js:23:9)
    at __webpack_require__ (C:\Users\**\Documents\GitHub\**\****\.next\server\webpack-runtime.js:25:42)
    at Object../server/lib/passport.js (C:\Users\**\Documents\GitHub\**\****\.next\server\pages_api_user_js-server_lib_passport_js-server_middleware_auth_js.js:245:73)
    at __webpack_require__ (C:\Users\**\Documents\GitHub\**\****\.next\server\webpack-runtime.js:25:42)
    at Object../server/middleware/auth.js (C:\Users\**\Documents\GitHub\**\****\.next\server\pages_api_user_js-server_lib_passport_js-server_middleware_auth_js.js:354:71)
    at __webpack_require__ (C:\Users\**\Documents\GitHub\**\****\.next\server\webpack-runtime.js:25:42)
    at Object../pages/dashboard.js (C:\Users\**\Documents\GitHub\**\****\.next\server\pages\dashboard.js:2865:81)
    at __webpack_require__ (C:\Users\**\Documents\GitHub\**\****\.next\server\webpack-runtime.js:25:42)
    at __webpack_exec__ (C:\Users\**\Documents\GitHub\**\****\.next\server\pages\dashboard.js:3251:52)
    at C:\Users\**\Documents\GitHub\**\****\.next\server\pages\dashboard.js:3252:358
    at Function.__webpack_require__.X (C:\Users\**\Documents\GitHub\**\****\.next\server\webpack-runtime.js:108:21)
    at C:\Users\**\Documents\GitHub\**\****\.next\server\pages\dashboard.js:3252:47

Here is the file that is causing the error:

import nextConnect from 'next-connect';
import auth from '../../server/middleware/auth';
const handler = nextConnect();

handler.use(auth).post(async (req, res) => {

 // Creates user and logs them in

  req.login(cleanUser(newUser), (err) => {
    if (err) throw err;
    res.status(201).json({
      success: true,
      user: cleanUser(newUser),
      message: 'Successfully signed up, you were automatically logged in!',
    });
  });
});

export default handler;

Here is the "auth" file:

import nextConnect from 'next-connect';
import passport from '../lib/passport';
import session from '../lib/session';

const auth = nextConnect()
  .use(
    session({
      name: 'sess',
      secret: process.env.TOKEN_SECRET,
      cookie: {
        maxAge: 60 * 60 * 8, // 8 hours,
        httpOnly: true,
        secure: process.env.NODE_ENV === 'production',
        path: '/',
        sameSite: 'lax',
      },
    })
  )
  .use(passport.initialize())
  .use(passport.session());

export default auth;

Any help I would appreciate, thanks.

izecubz commented 3 years ago

Some more info, I used this example when making this project: https://github.com/vercel/next.js/tree/canary/examples/with-passport-and-next-connect

izecubz commented 3 years ago

Fixed the issue, it was the importing of "auth" that did it.