1

I'm having a hard time understanding how to connect to the MongoDB Atlas Cluster from my react-native app. What I'm trying to do is basically take my data from my component login page (userName and password) and connect to the Atlas Cluster db to see if the data is there.

Im using React Native and use Expo to create the app. My login page opens up and I put in the data.

I want to take that data and then use the following code (from the Atlas Site Connection String) to connect and check.

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<userName>:<password>@testcluster1-dbdq3.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // perform actions on the collection object
  client.close();
});

Since react-native establishes a server, do I need to involve Express? Im new to this so I'm still trying to figure out what packages to utilize. Should I also install mongoose or mongoDB or both (from NPM). Im trying to wrap my head around how this works from a basic perspective and the packages required.

I want to perform a check against my userID and PW from my login page to the DB to see if the user exists. If the user doesnt, then I'll have them fill out some info and register which means writing a new user to my db.

So basically, I need to understand the code logic for:

  1. Connecting to the db through my app and when to perform this connection (when app loads or each time the login button is clicked)
  2. Take data from my userName and password and search the atlas db to see if the user exists. If so, then the next page loads.
  3. If username and password doesn't exist, then I write the new user and password to the db.

Thanks

Ray
  • 1,548
  • 2
  • 11
  • 18

2 Answers2

0

I think you should rewrite the code following the format suggested by mongodb here:

https://mongodb.github.io/node-mongodb-native/api-articles/nodekoarticle1.html

So essentially:

    const MongoClient = require('mongodb').MongoClient;

    //make sure to check connection string is correct here, since this depends on the whether you are running standalone, replica, sharded cluster 

    const uri = "mongodb+srv://<userName>:<password>@testcluster1-dbdq3.mongodb.net/test?retryWrites=true&w=majority";


    MongoClient.connect(uri, { useNewUrlParser: true }, function(err, client) {

       if (err) {

             //error

       } else {

             var collection = client.db('test').collection('devices');

             //client.close() should be called after you are done performing actions such as collection.update, etc.

       }
    });
DevKyle
  • 1,091
  • 6
  • 22
0

you can use any npm package with Expo if it works with RN (React Native), but you may need to detach in order to do so. Any npm packages which include native iOS or Android code will not work with Expo out of the box, unfortunately. Because MongoDB NPM package just mentioned the Node.js in thier docs, this doesn't mean that it will work on React Native. That's why MongoDB made this page about JUST React Native https://docs.mongodb.com/realm/tutorial/react-native/

You may need to use Realm Package to connect to MongoDB with React Native.

Maher Aldous
  • 894
  • 1
  • 10
  • 14