Javascript 使用react本机应用程序连接到MongoDB Atlas群集数据库
我很难理解如何从我的react原生应用程序连接到MongoDB Atlas集群。我试图做的基本上是从我的组件登录页面(用户名和密码)获取数据,然后连接到Atlas集群数据库,查看数据是否存在 Im使用React Native和Expo创建应用程序。我的登录页面打开,我输入了数据 我想获取这些数据,然后使用以下代码(来自Atlas站点连接字符串)进行连接和检查Javascript 使用react本机应用程序连接到MongoDB Atlas群集数据库,javascript,mongodb,react-native,mongoose,expo,Javascript,Mongodb,React Native,Mongoose,Expo,我很难理解如何从我的react原生应用程序连接到MongoDB Atlas集群。我试图做的基本上是从我的组件登录页面(用户名和密码)获取数据,然后连接到Atlas集群数据库,查看数据是否存在 Im使用React Native和Expo创建应用程序。我的登录页面打开,我输入了数据 我想获取这些数据,然后使用以下代码(来自Atlas站点连接字符串)进行连接和检查 const MongoClient = require('mongodb').MongoClient; const uri = "mong
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();
});
const MongoClient=require('mongodb')。MongoClient;
const uri=“mongodb+srv://:@testcluster1-dbdq3.mongodb.net/test?retryWrites=true&w=mailty”;
const client=newmongoclient(uri,{useNewUrlParser:true});
client.connect(err=>{
const collection=client.db(“测试”).collection(“设备”);
//对集合对象执行操作
client.close();
});
因为react native建立了一个服务器,我需要加入Express吗?我是新来的,所以我仍然在努力弄清楚应该使用哪些软件包。我是否也应该安装mongoose或mongoDB或两者(从NPM)。我试图从一个基本的角度和所需的软件包来理解这是如何工作的
我想从我的登录页面到数据库对我的userID和PW执行检查,以查看该用户是否存在。如果用户没有,我会让他们填写一些信息并注册,这意味着向我的数据库中写入一个新用户
因此,基本上,我需要理解以下代码逻辑:
谢谢我认为您应该按照mongodb建议的格式重写代码: 因此,本质上:
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.
}
});
const MongoClient=require('mongodb')。MongoClient;
//请确保检查此处的连接字符串是否正确,因为这取决于您是否正在运行独立、副本、分片群集
const uri=“mongodb+srv://:@testcluster1-dbdq3.mongodb.net/test?retryWrites=true&w=mailty”;
connect(uri,{useNewUrlParser:true},函数(err,客户端){
如果(错误){
//错误
}否则{
var collection=client.db('test')。collection('devices');
//应该在执行collection.update等操作后调用client.close()。
}
});
如果任何npm包与RN(React Native)一起使用,则可以将其与Expo一起使用,但您可能需要分离才能这样做。不幸的是,任何包含本机iOS或Android代码的npm软件包都不能与开箱即用的Expo一起使用。因为MongoDB NPM包刚刚在他们的文档中提到了Node.js,这并不意味着它将在React Native上工作。这就是为什么MongoDB制作了这个关于JUST React Native的页面
您可能需要使用Realm包连接到带有React Native的MongoDB