TypeError: Cannot read properties of undefined (reading 'startsWith')

I have this code here:

import { MongoClient } from "mongodb";

const createConnection = () => {

  // let current = null;

  const client = new MongoClient(process.env.MONGODB_URI);

  return async function run() {

    await client.connect();



export const managerdb = createConnection();

but it is giving me this error here:

TypeError: Cannot read properties of undefined (reading 'startsWith')

What do you think it could be wrong?

I am using this code for Nextjs

I am node a node.js guy, but perhaps MONGODB_URI is not defined

The name startsWith looks like a field from a $graphLookup stage.

I suspect that you have some logic error in the code that generates a dynamic $graphLookup with a null object.

I have doubts that the issue is in the connect code you shared.

The URI is good… tested and got connection OK from another file app code.

This error is trigger from a page requesting the connection from the utils page…

mport { managerdb } from "../utils/connect";

export const getStaticProps = async () => {

  const { db } = await managerdb.connect();

  const data = await db






  return {

    props: {

      data: JSON.parse(JSON.stringify(data)), // why?


    revalidate: 60,



The more I think about that issue the less confident I feel about my $graphLookup thing. If there was an issue with it, I suspect you would get a server error rather than a type error.


It would be helpful to see that URI since some application do not use exactly the same format.