TypeError:createSecureContext不是函数

时间:2019-04-19 18:22:23

标签: javascript node.js sql-server node-mssql

尝试在React中使用mssql

如果我使用

编写普通的js文件
var sql = require("mssql");

const pool = new sql.ConnectionPool({
  user: "sa",
  password: "password",
  server: "localhost", // You can use 'localhost\\instance' to connect to named instance
  database: "master",
  port: 50845,
  options: {
    trustedConnection: true
  }
});

var conn = pool;

conn
  .connect()
  .then(function() {
    var req = new sql.Request(conn);
    req
      .query("SELECT * FROM CRM.CRM.historical_data")
      .then(function(recordset) {
        console.log(recordset);
        conn.close();
      })
      .catch(function(err) {
        console.log(err);
        conn.close();
      });
  })
  .catch(function(err) {
    console.log(err);
  });

并使用node <filename.js>执行它,效果很好!

但是当我尝试在ReactJS中创建一个函数时,会抛出一个错误提示

  

TypeError: createSecureContext is not a function

下面的代码片段不起作用,但可以让您瞥见我的代码当前的样子。

class App extends React.Component {
  fetchData() {
    var sql = require("mssql");

    const pool = new sql.ConnectionPool({
      user: "sa",
      password: "password",
      server: "localhost", // You can use 'localhost\\instance' to connect to named instance
      database: "master",
      port: 50845,
      options: {
        trustedConnection: true
      }
    });

    var conn = pool;

    conn
      .connect()
      .then(function() {
        var req = new sql.Request(conn);
        req
          .query("SELECT * FROM CRM.CRM.historical_data")
          .then(function(recordset) {
            //WANT THIS CONSOLE TO SHOW THE DATA, as I can see in the file when I do `node <filename>`
            console.log(recordset);
            conn.close();
          })
          .catch(function(err) {
            console.log(err);
            conn.close();
          });
      })
      .catch(function(err) {
        console.log(err);
      });
  }

  render() {
    return (
      <div>
        <button onClick={() => this.fetchData()}>PRESS ME</button>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

0 个答案:

没有答案