TypeError: connectionString.startsWith is not a function

im facing this error in node.js, wen i try to add to the db. i have downloaded “controller”: “^1.0.0”, “cors”: “^2.8.5”, “dao”: “^0.0.4”,“dotenv”: “^16.0.3”, “express”: “^4.18.2”, “mongodb”: “^4.13.0”, “node-modules”: “^1.0.1”, “router”: “^1.3.7”

import app from './server.js';
import ReviewsDAO from "./dao/reviewsDAO.js";
import mongodb from 'mongodb';
import dotenv from 'dotenv';
const MongoClient = mongodb.MongoClient


//Load the environment variables from the .env file
dotenv.config();
//Access the environment variables

const mongo_username = process.env.MONGO_USERNAME;
const mongo_password = process.env.MONGO_PASSWORD;
//access to the db 
const url = new MongoClient(`mongodb+srv://${mongo_username}:${mongo_password}@cluster0.b5f7prn.mongodb.net/test`);
 
const port=4444;

MongoClient.connect(
    url,
    {
      maxPoolSize: 50,
      wtimeoutMS: 2500,
      useNewUrlParser: true
    })
    .catch(err => {
      console.error(err.stack)
      process.exit(1)
    })
    .then(async client => {
      await ReviewsDAO.injectDB(client)
      app.listen(port, () => {
        console.log(`listening on port ${port}`)
      })
    })

It sounds like there is some error inside the mongo module. I would start at the top and just start logging out all the variables, one by one, and find where you start getting something weird.

1 Like

Thank you for responding , the error is appear here idk why.

i just passed mongodb uri to MongoClient.connect and its worked just fine.


MongoClient.connect(`mongodb+srv://${mongo_username}:${mongo_password}@cluster0.b5f7prn.mongodb.net/test`,
{
  maxPoolSize: 50,
  wtimeoutMS: 2500,
  useNewUrlParser: true
})