3

I'm trying to import mssql I have a js file that works fine by it's self but I need to do it so that I can call the function from within a react js environment rather than just node

import React, { Component } from 'react';

import TextInput from '../SmallBits/FormItems/TextInput';

import sql from 'mssql';
//or
var sql = require('mssql');

var dbConfig = {
    server: 'localhost',
    database: 'TestDB',
    user: 'andy',
    password: 'andy',
    port: 1433
};

export default class LeftBox extends Component {
    constructor(props){
        super(props);
        this.state = {
        name: '',
        forename: '',
        surname: '',
        caseID: '',
        address: ''
    }
    this.handleSurnameChange = this.handleSurnameChange.bind(this);
    this.handleForenameChange = this.handleForenameChange.bind(this);
    this.handleCaseIDChange = this.handleCaseIDChange.bind(this);
    this.handleAddressChange = this.handleAddressChange.bind(this);
}

loadEmployees() {
    var dbConn = new sql.ConnectionPool(dbConfig);
    dbConn.connect().then(function () {
        var request = new sql.Request(dbConn);
        request.query("select * from EmployeeTable").then(function (recordSet) {
            console.log(recordSet);
            return recordSet;
            dbConn.close();
        }).catch(function (err) {
            console.log(err);
            dbConn.close();
        });
    }).catch(function (err) {
        console.log(err);
    });
}

That's all the code that is being called both the import and require statement at the top bot give me the same error that Module not found: Can't resolve 'mssql' in 'C:\Users\wilsona.CHECKMATE\Desktop\reactapp\Workspace\savvy-crm-test\src\components\Parts\Boxes' even though mssql is installed globally should I install locally aswell

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
andy wilson
  • 920
  • 5
  • 16
  • 38

1 Answers1

3

This seems similar to Errors with node-mssql in a webpack-bundled React app

MSSQL server connection libraries are not client-side javascript libraries. There is absolutely no way you can make TCP SQL connections from a web browser, even if you wrote your own library.

For security reasons, you would never want to allow client-side access to the SQL Server. You need to spin up server-side environment (e.g. Node/Express) to expose an API to the client applications.

  • I am running node though would i need express too – andy wilson Jun 30 '17 at 08:38
  • React is used for server side rendering which means you can't make SQL calls from react components. If you want to do that you need to pass your request to a server via something like socket.io or API calls then have the server handle the database transaction. Here is a site that should give you an idea of what you need: https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/ – papasmurf279 Jun 30 '17 at 14:11