3

I have a react website that should query data from my mongodb atlas database and display it to the user. In my atlas cluster, I already created a user readuserwith the password readuser and readall-permissions.

Following the official guide, I try this:

import React from 'react';
import MongoClient from 'mongodb';

function App() {
    var uri = "mongodb+srv://readuser:readuser@personalwebsite-oerah.mongodb.net/test?retryWrites=true&w=majority";
    var client = new MongoClient(uri, {useNewUrlParser : true});
    client.connect(err => {
        console.log(err);
        client.close();
    });
    return (<p>Hello world</p>);
}

export default App;

However, react displays the error Uncaught TypeError: Cannot read property 'replace' of undefined at matchesParentDomain (uri_parser.js:24)

  1. Is the node js solution fitting for my problem? (It seems to be intended more for backend applications)
  2. Is it possible to make my database publicly readable to avoid needing to login from my app?
  3. How do I fix the error?

I know this sounds very similar to this thread, but I don't use mongoose, so the problem is a little different.

Jan Berndt
  • 913
  • 1
  • 10
  • 22

1 Answers1

0

the example given on the official website is for nodejs and not react(clint side javascript). you would need to create a backend API endpoint with nodejs or PHP or any other serverside script, then do a simple fetch with react to get the data from your own endpoint. it's also safer this way since you don't have to expose your db credentials on clientside javascript.

Madi s
  • 91
  • 5