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.

A post was split to a new topic: Cannot read properties of undefined (reading ‘type’)

Error: Cannot read properties of undefined (reading 'connection')
[nodemon] app crashed - waiting for file changes before starting...

const mongoose = require("mongoose");
const connectDB = async () => {
  const conn = await mongoose
    .connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    .catch((err) => {
      console.log("error: ", err);

  console.log(`Mongoose Connected: ${conn.connection.host}`.cyan.underline.bold);

module.exports = connectDB;

Hello, But how to do it in nodejs