surmon-china / mongodb-data-api

MongoDB Atlas Data API SDK for @nodejs
https://www.mongodb.com/docs/atlas/api/data-api/
MIT License
40 stars 8 forks source link

running the lib in nextJS middleware #6

Open Ezall2001 opened 1 year ago

Ezall2001 commented 1 year ago

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

fbjork commented 1 year ago

Also running into this issue. @surmon-china any ideas how to fix?

surmon-china commented 1 year ago

error.toJSON() It is likely that the error is caused by the internal error handling logic, can you provide a more detailed error stack? It is better to reflect the data type of the error itself.

fbjork commented 1 year ago

@surmon-china webpack-internal:/(sc_server)/node_modules/mongodb-data-api/dist/mongodb-data-api.esm.js (117:31).

Here is the code:

import { createMongoDBDataAPI } from 'mongodb-data-api'

const config = {
  apiKey: process.env.MONGODB_API_KEY as string,
  urlEndpoint: process.env.MONGODB_APP_URL as string
}

const mongo = createMongoDBDataAPI(config)

const res = await mongo.$cluster(process.env.MONGODB_CLUSTER).$database(process.env.MONGODB_DB).$collection('posts').find()
surmon-china commented 1 year ago

Hi! @Ezall2001 @fbjork

I think this issue has been fixed in v0.3.0 (please note there are some breaking change)

fbjork commented 1 year ago

@surmon-china I just tried it, but now I get a new error:

Error: Cannot read properties of undefined (reading 'headers')
Call Stack
eval

webpack-internal:/(sc_server)/node_modules/mongodb-data-api/dist/mongodb-data-api.esm.js (109:30)
surmon-china commented 1 year ago

@surmon-china I just tried it, but now I get a new error:

Error: Cannot read properties of undefined (reading 'headers')
Call Stack
eval

webpack-internal:/(sc_server)/node_modules/mongodb-data-api/dist/mongodb-data-api.esm.js (109:30)

Try v0.4.0 (API key desensitization is no longer processed when an error occurs.)

fbjork commented 1 year ago

@surmon-china now I'm getting this error:


  message: 'Adapter http is not supported by the environment',
  name: 'AxiosError',
  description: undefined,
  number: undefined,
  fileName: undefined,
  lineNumber: undefined,
  columnNumber: undefined,
  stack: 'AxiosError: Adapter http is not supported by the environment\n    at Object.getAdapter (webpack-internal:///(sc_server)/./node_modules/axios/dist/node/axios.cjs:2598:23)\n    at Axios.dispatchRequest (webpack-internal:///(sc_server)/./node_modules/axios/dist/node/axios.cjs:2641:30)\n    at Axios.request (webpack-internal:///(sc_server)/./node_modules/axios/dist/node/axios.cjs:2933:39)\n    at MongoDBDataAPI.wrap (webpack-internal:///(sc_server)/./node_modules/axios/dist/node/axios.cjs:28:19)\n    at MongoDBDataAPI.$$action (webpack-internal:///(sc_server)/./node_modules/mongodb-data-api/dist/mongodb-data-api.esm.js:93:69)\n    at MongoDBDataAPI.find (webpack-internal:///(sc_server)/./node_modules/mongodb-data-api/dist/mongodb-data-api.esm.js:119:17)\n    at Page (webpack-internal:///(sc_server)/./app/mongodb/page.tsx:15:71)\n    at preloadComponent (webpack-internal:///(sc_client)/./node_modules/next/dist/esm/server/app-render.js:62:22)\n    at eval (webpack-internal:///(sc_client)/./node_modules/next/dist/esm/server/app-render.js:801:24)\n    at async createComponentTree (webpack-internal:///(sc_client)/./node_modules/next/dist/esm/server/app-render.js:800:25)',
  config: undefined,
  code: 'ERR_NOT_SUPPORT',
  status: null
}```
surmon-china commented 1 year ago

@surmon-china now I'm getting this error:

  message: 'Adapter http is not supported by the environment',
  name: 'AxiosError',
  description: undefined,
  number: undefined,
  fileName: undefined,
  lineNumber: undefined,
  columnNumber: undefined,
  stack: 'AxiosError: Adapter http is not supported by the environment\n    at Object.getAdapter (webpack-internal:///(sc_server)/./node_modules/axios/dist/node/axios.cjs:2598:23)\n    at Axios.dispatchRequest (webpack-internal:///(sc_server)/./node_modules/axios/dist/node/axios.cjs:2641:30)\n    at Axios.request (webpack-internal:///(sc_server)/./node_modules/axios/dist/node/axios.cjs:2933:39)\n    at MongoDBDataAPI.wrap (webpack-internal:///(sc_server)/./node_modules/axios/dist/node/axios.cjs:28:19)\n    at MongoDBDataAPI.$$action (webpack-internal:///(sc_server)/./node_modules/mongodb-data-api/dist/mongodb-data-api.esm.js:93:69)\n    at MongoDBDataAPI.find (webpack-internal:///(sc_server)/./node_modules/mongodb-data-api/dist/mongodb-data-api.esm.js:119:17)\n    at Page (webpack-internal:///(sc_server)/./app/mongodb/page.tsx:15:71)\n    at preloadComponent (webpack-internal:///(sc_client)/./node_modules/next/dist/esm/server/app-render.js:62:22)\n    at eval (webpack-internal:///(sc_client)/./node_modules/next/dist/esm/server/app-render.js:801:24)\n    at async createComponentTree (webpack-internal:///(sc_client)/./node_modules/next/dist/esm/server/app-render.js:800:25)',
  config: undefined,
  code: 'ERR_NOT_SUPPORT',
  status: null
}```

This looks like an axios request error caused by the runtime environment, can you provide your system info? (npx envinfo --system --npmPackages --binaries --browsers)

fbjork commented 1 year ago

@surmon-china here goes. Also note that I'm using the edge runtime with my Next.js project.

  System:
    OS: macOS 13.0.1
    CPU: (6) x64 Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
    Memory: 844.29 MB / 32.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.15.0/bin/yarn
    npm: 8.14.0 - ~/.nvm/versions/node/v16.15.0/bin/npm
  Browsers:
    Brave Browser: 103.1.40.105
    Chrome: 107.0.5304.110
    Safari: 16.1
  npmPackages:
    @graphql-codegen/cli: 2.13.12 => 2.13.12
    @graphql-codegen/client-preset: 1.1.5 => 1.1.5
    @planetscale/database: ^1.4.0 => 1.4.0
    @tailwindcss/typography: 0.5.8 => 0.5.8
    @types/node: 18.11.9 => 18.11.9
    @types/react: 18.0.25 => 18.0.25
    @types/react-dom: 18.0.9 => 18.0.9
    @xata.io/client: ^0.21.2 => 0.21.2
    autoprefixer: 10.4.13 => 10.4.13
    graphql: 16.6.0 => 16.6.0
    graphql-request: 5.0.0 => 5.0.0
    mongodb-data-api: 0.4.0 => 0.4.0
    next: 13.0.5 => 13.0.5
    postcss: 8.4.19 => 8.4.19
    react: 18.2.0 => 18.2.0
    react-dom: 18.2.0 => 18.2.0
    tailwindcss: 3.2.4 => 3.2.4
    typescript: 4.9.3 => 4.9.3
fbjork commented 1 year ago

@surmon-china anything else I can share to help fix this?