在React App中与MongoDB的简单连接 [英] Simple connection to mongodb in react app

查看:362
本文介绍了在React App中与MongoDB的简单连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用"create-react-app"创建了简单的react应用.该应用程序包含表单,验证和引导程序.没有什么幻想能像魅力一样.

I have created simple react app using 'create-react-app'. This app contains form, validation and bootstrap things. Nothing fancy yet works like a charm.

我还注册了mongo以获得免费集群,因此我可以向其中发送一些数据.所以我有这个网址:

I have also signed up to mongo to get a free cluster so I can send over some data to. So I have this url:

mongodb+srv://matt:passwprd@cluster0-jlasm.mongodb.net/test

现在,我要做的就是将JSON数据从表单发送到mongo,但我不知道如何.

Now, all I want to do is to sent json data from the form to mongo but I don't know how.

当我遵循教程并安装mongodb,mongoose或任何软件包并为将来的CRUD操作添加基本设置时:

When I am following tutorials and installing mongodb, mongoose or whatever packages and adding basic setup for future CRUD operations:

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb+srv://mattOsuch:brainhub123@cluster0-jlasm.mongodb.net/test';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  db.close();
});

整个应用程序崩溃:

./node_modules/mongodb-core/lib/uri_parser.js
Module not found: Can't resolve 'dns' in 'C:\Users\Mateusz\Desktop\brainhub\node_modules\mongodb-core\lib'

我曾经在PHP中使用jquery或mysql_query发送数据,但是我无法克服这个问题.换句话说,我想实现本视频中介绍的功能: https://www.youtube.com /watch?v = Jsqz5op4fH8 正如我所说,简单的数据更新.

I used to send data using jquery or mysql_query in PHP but I can't overcome this problem. In other words I want to achieve functionality like presented in this video: https://www.youtube.com/watch?v=Jsqz5op4fH8 So as I said, simple data update.

我怀疑react-scripts服务器侦听器与mongo有某种冲突,但我不确定.

My suspicion is that react-scripts server listener has some sort of conflict with mongo but I am not sure.

请帮助我,因为我放松了神经.

Please help me beceause I am loosing my nerves.

推荐答案

您正在使用node.js,因此启动服务器应用程序尝试使用快速路由,此处是指向教程

You are using node.js so start server app try using express routing here is a link to a tutorial https://zellwk.com/blog/crud-express-mongodb or https://codeburst.io/hitchhikers-guide-to-back-end-development-with-examples-3f97c70e0073 or try doing a google search(node.js mongodb and express).

然后,当从服务器返回请求时,发送所需的数据,然后使用您的react客户端处理接收到的数据

Then when returning a request from server send the data required then use your react client to handle the data recived

这篇关于在React App中与MongoDB的简单连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆