0

I'm trying to connect to mongodb in my react app but it throws error:

Unable to get property 'replace' of undefined or null reference

the error occurs when I console.log(err) in the callback of the connect method

import React from 'react';
import {MongoClient} from "mongodb";

function App(){
    // user, pass, cluster, and db are replaced in my actual code
    const uri = "mongodb+srv://<user>:<pass>@<cluster>.mongodb.net/<db>?retryWrites=true&w=majority";

    MongoClient.connect(uri, {useNewUrlParser: true}, (err, db) => {
        if (err) console.log(err.message);
    }

    return (
        // html markup
    );
}

export default App;

I am unfamiliar with this error as I have never accoutered this before

Steve Coulter
  • 61
  • 2
  • 9

1 Answers1

0

Would be useful to see the whole code(especially where the replace happens), but this error means that you're trying to do something like this:

const a = null;
a.replace()...
Tom Weiss
  • 116
  • 1
  • 4
  • that's the problem, that is the whole code.. im thinking the error is from the mongodb module? – Steve Coulter Aug 04 '19 at 00:00
  • Hmm, is there any other place where the credentials are stored? (some config file?), perhaps [this](https://stackoverflow.com/questions/50033010/mongoose-5-0-16-getting-cannot-read-property-replace-of-undefined) issue is similar to yours - try removing the username and password from URL if you it have in a config. Also, I'm not sure which mongoclient version you have but if it's not the newest I would check if upgrading it helps. – Tom Weiss Aug 05 '19 at 15:00