首页 > 解决方案 > 我在构造函数中创建了一个连接,但在保存组件时出现错误

问题描述

我已经正确导入了mysql。这是我的代码,其中我在构造函数中声明了连接。

import mysql from 'mysql';
export class StepForm extends Component {
    constructor() {
        super();
        this.state ={
            step: 1, 
            // welcome
            qNumber:1,
            accountNumber:'',
            amount:'',
            txNumber:1,
            postArray : []
        }
        const connection = mysql.createConnection({
            host: 'localhost',
            port: 3306,
            user: 'root',
            password: 'whil',
            database: 'qany'
        });
        connection.connect();
        console.log(connection);
    }

保存组件时遇到错误,

TypeError: Net.createConnection is not a function
Connection.connect
C:/react_programs/q_any/node_modules/mysql/lib/Connection.js:74
  71 | this._connectCalled = true;
  72 | 
  73 | // Connect either via a UNIX domain socket or a TCP socket.
> 74 | this._socket = (this.config.socketPath)
     | ^  75 |   ? Net.createConnection(this.config.socketPath)
  76 |   : Net.createConnection(this.config.port, this.config.host);
  77 | 

应该做什么?

标签: mysqlreactjsconnection

解决方案


推荐阅读