Error with await client.connect() node.JS

Hi all,

I hope whoever is reading this is having a great day! I am currently trying to connect my mongodb Atlas to my React app using node.JS but I am getting the error TypeError: Cannot read property 'replace' of undefined at matchesParentDomain (uri_parser.js:24) at uri_parser.js:67

I have posted my (extremely simple) code below :stuck_out_tongue:

const {MongoClient} = require('mongodb');

    async function main(){
        const uri = "mongodb+srv://{username}:{password}@cluster0.5r4og.mongodb.net/{dbname}?retryWrites=true&w=majority";
     

        const client = await new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true});
     
        try {
            await client.connect();
     
        } catch (e) {
            console.error(e);
        } finally {
            await client.close();
        }
    }

    main().catch(console.error);

Hello @Tatiana_Wiener, you can substitute the actual username , password and the dbname in place of {username}, etc. For example,

"mongodb+srv://user001:secret123@cluster0.5r4og.mongodb.net/test?retryWrites=true&w=majority

Hi! Thanks for the reply! I do have my username, password, and Db name in the URI, I just omitted it for posting reasons :slight_smile:

The code worked fine from my laptop using a NodeJS (version 12) app. I was able to connect to my Atlas Cluster account using similar URI.

Hi there, I figured out it’s whenever I have any other outside functions. For example. I am trying to connect my MongoDB in another React function using the same methods:

const MongoClient = require('mongodb').MongoClient;    
export default function AboutUsPage(){
      React.useEffect(() => {
        window.scrollTo(0, 0);
        document.body.scrollTop = 0;
      });
      const classes = useStyles();
      const uri = "mongodb+srv://{Username}:{Password}@cluster0.5r4og.mongodb.net/{DB}?retryWrites=true&w=majority";
      const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true});
      connect().catch(console.error);
      
      async function connect() {
        await client.connect();

      }

I am getting the same error TypeError: Cannot read property 'replace' of undefined Is this because I’m trying to connect it within an exported function?? Like I literally have no idea why I can’t connect to it at all. I’ve tried without the async function as well and get the same thing.

Lastly, I have tried literally copy and pasting my previous main() function into a new document and simply calling main() from my new export default function AboutUsPage(){ and it still doesn’t work.

A post was split to a new topic: TypeError: Cannot read property ‘replace’ of undefined matchesParentDomain