I am trying to connect to mongodb using mongoose and a connection string from MongoDB atlas but I keep getting the error below:
TypeError: Cannot read properties of undefined (reading 'constructor')
I installed the latest version(8.7.3) and downgraded it to 7.6.0, which I used in a previous project. Both gave the same error.
Reading the connection string from .env
and hard-coding it gave the same error.
Here's the code that is responsible for the connection. Any help will be appreciated
import dotenv from 'dotenv';import mongoose from 'mongoose';import variables from '../variables';import { logger } from '../utilities';dotenv.config();console.log('DB_URI', process.env.DATABASE_URL);export const connect = async (): Promise<void> => { try { if (!process.env.DATABASE_URL) { throw new Error('DATABASE_URL not provided'); } await mongoose.connect("mongodb+srv://username:password@cluster0.5n5aj.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"); console.log('DB connected successfully'); logger.info(`DB is successfully connected`); } catch (error) { console.log('DB connection error>>', error); logger.error(`DB connection error ${error}`); }};export const disconnect = () => { if (!process.env.DATABASE_URL) { return; } mongoose.disconnect();};