如果我需要,我必须做什么:在用户单击按钮add
后,我想将POST方法引入数据库MySQL。我必须从server.js以外的其他文件连接到数据库,但是我收到错误
Net.createConnection is not a function
这是我的Compontent文件
import React, { PropTypes} from 'react';
import { connect } from 'react-redux';
import { CounterActions } from '../redux/actions';
import { Link } from 'react-router'
import mysql from 'mysql'
var connection = mysql.createConnection({
host : 'xxx',
user : 'xxx',
password : 'xxx',
database : 'xxx'
});
connection.connect(function(error){
if(!!error){
console.log('error');
}else{
console.log('Connected to db');
}
})
class App extends React.Component{
constructor(props){
super(props);
}
click(){
this.props.testClick();
}
render(){
return (
<div>
<h1>Hallow Reduxasd <div></div></h1>
<h2>Counter: {this.props.global.counter}</h2>
<button onClick={this.click.bind(this)}>Click Me!</button>
<Link to="/test">Test</Link>
<h3><Link to={'/user/'+this.props.global.counter}>{'Link do user id: '+this.props.global.counter}</Link></h3>
</div>
)
}
}