简体   繁体   English

如何在node.js上将reactJS与mysql连接起来

[英]How to connect reactJS with mysql on node.js

I am trying to insert form data from reactJS to mysql database on Node.js. 我试图将reactJS中的表单数据插入Node.js上的mysql数据库。 I am beginner in reactJS so please help me. 我是reactJS的初学者,所以请帮助我。

import React from 'react';
import Layout from './App.jsx';

class Header extends React.Component{

constructor(props) {
  super(props);
  this.handleSubmit = this.handleSubmit.bind(this);
}

handleSubmit(event) {
 var fname=this.refs.fname.value; //This variable holds firstname 
}
render() {
  return (
    <div>
      <form onSubmit={this.handleSubmit}>
      <h1>Registration Form: </h1>
      <label>
       First Name:
       <input type="text" ref="fname" value={this.state.fname}  /><br/><br/>
      </label><br/>
      <label>
       Last Name:
       <input type="text" ref="lname" value={this.state.lname}  /><br/><br/>
      </label><br/>
    <input type="submit" value="Submit" />      
  </form>
 </div> 
);
 }
}
export default Header;

Above code is Header.jsx which renders form 上面的代码是Header.jsx ,它呈现了表单

The variable fname contains value which I want to insert into mysql database. 变量fname包含我要插入mysql数据库的值。

Well, the answer of your question may go deep into flux (or redux) architecture and talking about services, dispatching actions and so on. 那么,你的问题的答案可能会深入到通量(或redux)架构和谈论服务,调度行动等等。 If you want to do it properly then you'll probably need to learn about this stuff. 如果你想做得好,那么你可能需要了解这些东西。 If you want to do it quickly then you may pass whatever you want to the component via props. 如果你想快速完成,那么你可以通过道具将你想要的任何东西传递给组件。 For example: 例如:

var storeToDatabase = function(username) {
  // here you call a backend endpoint with some library like superagent
}

<Header store={ storeToDatabase } />

// and then in your Header.jsx component
handleSubmit(event) {
 var fname = this.refs.fname.value;

 this.props.store({ fname });
}

So, React is only about rendering views. 所以,React只是关于渲染视图。 All the other stuff should be handle outside of it. 所有其他的东西应该在它之外处理。

It seems you are a little confused. 看来你有点困惑。 First of all, what you want to do is to send a request to a Node.js server, which will handle the request and save it to the database. 首先,您要做的是向Node.js服务器发送请求,该服务器将处理请求并将其保存到数据库。 You don't connect React to MySQL. 你没有将React连接到MySQL。

To do this you could install a library to make the request, eg superagent . 要做到这一点,你可以安装一个库来发出请求,例如superagent You import it: import request from 'superagent' . 你导入它: import request from 'superagent' Then you should do something like this: 然后你应该做这样的事情:

handleSubmit(event) {
  var fname=this.refs.fname.value; //This variable holds firstname
  request.post('/api/name').send({ name: fname }).end(callback);
}

Where callback is a function which will receive the server response and you can do whatever you want with it. callback是一个接收服务器响应的函数,你可以用它做任何你想做的事情。

In the server, if you are for exampling using Express, you should do something like this: 在服务器中,如果您要使用Express进行示例,则应该执行以下操作:

app.post('/api/name', function(req, res) {
  // code for saving the name in the database
})

By the way, you don't need Redux store (which Krasimir said). 顺便说一下,你不需要Redux商店(Krasimir说)。 This has nothing to do with Redux and application state. 这与Redux和应用程序状态无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM