Skip to content

running the lib in nextJS middleware #6

@Ezall2001

Description

@Ezall2001

Describe the bug

I am getting:
error - (middleware)\node_modules\mongodb-data-api\dist\mongodb-data-api.esm.js (106:0) @ eval
error - error.toJSON is not a function

Reproduction

-> mongodbDataApi.ts
`
import { createMongoDBDataAPI } from 'mongodb-data-api'

const connectToMongoAPI = (collection: string) => {
if (
!process.env.MONGODB_API_URL ||
!process.env.MONGODB_API_KEY ||
!process.env.MONGODB_CLUSTER ||
!process.env.MONGODB_DB_NAME
)
return undefined

return createMongoDBDataAPI({
apiKey: process.env.MONGODB_API_KEY,
urlEndpoint: process.env.MONGODB_API_URL,
})
.$cluster(process.env.MONGODB_CLUSTER)
.$database(process.env.MONGODB_DB_NAME)
.$collection(collection)
}

export default connectToMongoAPI
`
-> middleware.ts

`
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { getToken } from 'next-auth/jwt'

import connectToMongoAPI from 'lib/mongodbDataApi'

export const middleware = async (req: NextRequest) => {
const session = await getToken({ req, secret: process.env.SECRET })

/// auth Layer
if (
!session &&
!(
req.nextUrl.pathname.startsWith('/auth/login') ||
req.nextUrl.pathname.startsWith('/api/auth')
)
)
return NextResponse.redirect(new URL('/auth/login', req.url))

/// role layer
const res = await connectToMongoAPI('Pages')?.find({ filter: {} })
console.log(res)
}

export const config = {
matcher: ['/((?!_next|favicon.ico).*)'],
}

`

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (16) x64 Intel(R) Core(TM) i7-10870H CPU @ 2.20GHz
    Memory: 4.26 GB / 15.79 GB
  Binaries:
    Node: 16.16.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.17.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (105.0.1343.50)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @babel/core: ^7.18.10 => 7.18.10
    @commitlint/cli: ^17.0.3 => 17.0.3
    @commitlint/config-conventional: ^17.0.3 => 17.0.3
    @emotion/react: ^11.10.4 => 11.10.4
    @emotion/styled: ^11.10.4 => 11.10.4
    @mui/icons-material: ^5.10.3 => 5.10.3
    @mui/lab: ^5.0.0-alpha.99 => 5.0.0-alpha.99
    @mui/material: ^5.10.5 => 5.10.5
    @next-auth/mongodb-adapter: ^1.0.4 => 1.0.4
    @reduxjs/toolkit: ^1.8.4 => 1.8.4
    @storybook/addon-a11y: ^6.5.10 => 6.5.10
    @storybook/addon-actions: ^6.5.10 => 6.5.10
    @storybook/addon-essentials: ^6.5.10 => 6.5.10
    @storybook/addon-interactions: ^6.5.10 => 6.5.10
    @storybook/addon-links: ^6.5.10 => 6.5.10
    @storybook/builder-webpack5: ^6.5.10 => 6.5.10
    @storybook/manager-webpack5: ^6.5.10 => 6.5.10
    @storybook/react: ^6.5.10 => 6.5.10
    @storybook/testing-library: ^0.0.13 => 0.0.13
    @types/bcrypt: ^5.0.0 => 5.0.0
    @types/node: 18.7.1 => 18.7.1
    @types/react: 18.0.17 => 18.0.17
    @types/react-dom: 18.0.6 => 18.0.6
    @typescript-eslint/eslint-plugin: ^5.33.0 => 5.33.0
    @typescript-eslint/parser: ^5.33.0 => 5.33.0
    babel-loader: ^8.2.5 => 8.2.5
    babel-preset-next: ^1.4.0 => 1.4.0
    bcrypt: ^5.0.1 => 5.0.1
    cross-env: ^7.0.3 => 7.0.3
    eslint: ^8.21.0 => 8.21.0
    eslint-config-next: 12.2.4 => 12.2.4
    eslint-plugin-check-file: ^1.2.2 => 1.2.2
    eslint-plugin-prefer-arrow: ^1.2.3 => 1.2.3
    eslint-plugin-storybook: ^0.6.4 => 0.6.4
    husky: ^8.0.0 => 8.0.1
    lint-staged: ^13.0.3 => 13.0.3
    mongodb: ^4.8.1 => 4.8.1
    mongodb-data-api: ^0.2.0 => 0.2.0
    mongoose: ^6.5.2 => 6.5.2
    next: ^12.3.1 => 12.3.1
    next-auth: ^4.10.3 => 4.10.3
    next-redux-wrapper: ^8.0.0 => 8.0.0
    prettier: ^2.7.1 => 2.7.1
    react: 18.2.0 => 18.2.0
    react-dom: 18.2.0 => 18.2.0
    react-hook-form: ^7.34.2 => 7.34.2
    react-redux: ^8.0.2 => 8.0.2
    typescript: ^4.7.4 => 4.7.4
    validate-branch-name: ^1.3.0 => 1.3.0

Used Package Manager

npm

Validations

  • Read the the documentation in detail.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
  • The provided reproduction is a minimal reproducible example of the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions