最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Node.JsElectron using mssql module to insert form data into a table - Stack Overflow

matteradmin7PV0评论

I'll preface this with a I am noob.

I am trying to insert data into my MSSQL table using javascript. I am using mssql module, which I successfully used to read data, so I know it works, or at least the connection works enough for me to read it.

So basically I have a form with 2 inputs first and last name, and then you click submit and it will add those names to the form. I can't seem to get this to happen. I feel I am missing some part of the documentation, but I can't youtube or find an answer here yet.

The offending code:

<!DOCTYPE html>
<html lang="en">

<head>

<title>Add New User</title>
</head>

<body>
<h1>Add New User</h1>
<div class="container">
    <form id="newUser">
        <label>First Name</label>
        <input type="text" id="newUsrFirstName"></input>
        <label>Last Name</label>
        <input type="text" id="newUsrLastName"></input>
        <br>
        <button type="submit">Submit</button>

    </form>
</div>
<script>Here is my javascript</script>

Then the offending javascript:

<script type="text/javascript">
    const electron = require('electron');
    const mssql = require('mssql');
    //Gather the data
    const form = document.querySelector('form');
    form.addEventListener('submit', submitForm);

    function submitForm(e) {
        e.preventDefault();
        const firstName = document.querySelector('#newUsrFirstName').value;
        const lastName = document.querySelector('#newUsrLastName').value;
        //console.log(firstName, lastName);
        //Attempt to send to Server MSSQL
        //Database Details not secure but eh, internal app
        const config = {
            user: 'username',
            password: 'password',
            server: 'thedevilswork\\SQLExpress',
            database: 'nirvana',
        };
        var addConn = (async function() {
            try {
                let pool = await sql.connect(config);
                let results = await pool.request()
                return await pool.request().query('insert into dbo.nirvana (FirstName, LastName, AmountDuePrevious, AmountDueCurrent) values (lastName , firstName , 0, 0);');
                //.query('insert into dbo.nirvana (FirstName, LastName, AmountDuePrevious, AmountDueCurrent) values ('+lastName+' , '+firstName+' , 0, 0);');
                console.log(results);
            } catch (err) {
                console.log(err);
            }
        });
    }
</script>

Be kind I am learning still so I apologise if this is obvious.

I'll preface this with a I am noob.

I am trying to insert data into my MSSQL table using javascript. I am using mssql module, which I successfully used to read data, so I know it works, or at least the connection works enough for me to read it.

So basically I have a form with 2 inputs first and last name, and then you click submit and it will add those names to the form. I can't seem to get this to happen. I feel I am missing some part of the documentation, but I can't youtube or find an answer here yet.

The offending code:

<!DOCTYPE html>
<html lang="en">

<head>

<title>Add New User</title>
</head>

<body>
<h1>Add New User</h1>
<div class="container">
    <form id="newUser">
        <label>First Name</label>
        <input type="text" id="newUsrFirstName"></input>
        <label>Last Name</label>
        <input type="text" id="newUsrLastName"></input>
        <br>
        <button type="submit">Submit</button>

    </form>
</div>
<script>Here is my javascript</script>

Then the offending javascript:

<script type="text/javascript">
    const electron = require('electron');
    const mssql = require('mssql');
    //Gather the data
    const form = document.querySelector('form');
    form.addEventListener('submit', submitForm);

    function submitForm(e) {
        e.preventDefault();
        const firstName = document.querySelector('#newUsrFirstName').value;
        const lastName = document.querySelector('#newUsrLastName').value;
        //console.log(firstName, lastName);
        //Attempt to send to Server MSSQL
        //Database Details not secure but eh, internal app
        const config = {
            user: 'username',
            password: 'password',
            server: 'thedevilswork\\SQLExpress',
            database: 'nirvana',
        };
        var addConn = (async function() {
            try {
                let pool = await sql.connect(config);
                let results = await pool.request()
                return await pool.request().query('insert into dbo.nirvana (FirstName, LastName, AmountDuePrevious, AmountDueCurrent) values (lastName , firstName , 0, 0);');
                //.query('insert into dbo.nirvana (FirstName, LastName, AmountDuePrevious, AmountDueCurrent) values ('+lastName+' , '+firstName+' , 0, 0);');
                console.log(results);
            } catch (err) {
                console.log(err);
            }
        });
    }
</script>

Be kind I am learning still so I apologise if this is obvious.

Share Improve this question edited Nov 9, 2017 at 3:20 Shaunak 18.1k5 gold badges56 silver badges86 bronze badges asked Nov 9, 2017 at 1:51 AchmannAchmann 1961 silver badge11 bronze badges 2
  • The code executes without an error but you don't see you row? or you see an error on console? – Shaunak Commented Nov 9, 2017 at 3:23
  • No I see no error, which confuses me, I also see no new row in my database. I feel a little lost without error messages – Achmann Commented Nov 9, 2017 at 4:39
Add a ment  | 

2 Answers 2

Reset to default 4

I dont know if its too late. But the problem is here:

const mssql = require('mssql');
........^.........v.........................
let pool = await sql.connect(config);

Change it to:

let pool = await mssql.connect(config);

OK I seem to have convinced it to work. I needed to keep adding const = require ('mssql'); a bunch which I don't fully prehend why. I will post the full bottom section to my code in the hope that this question es up for someone else in the future.

Here is the revised code, I also spent a bunch of time figuring out the sql query so that it would accept my variables properly. This was trail and error and very messy so I do not remend my particular method to arrive at the result.

 <script type="text/javascript">

        const electron = require('electron');
        const mssql = require('mssql');

        //Gather the data
        const form = document.querySelector('form');
        form.addEventListener('submit', submitForm);
        function submitForm(e) {

            e.preventDefault();
            const firstName = document.querySelector('#newUsrFirstName').value;
            const lastName = document.querySelector('#newUsrLastName').value;
            //console.log(firstName, lastName);

            //Attempt to send to Server MSSQL
            //Database Details not secure but eh, internal app
            const config = {
                user: 'username',
                password: 'password',
                server: 'devilsworkstation\\SQLExpress',
                database: 'nirvana',
            };

            var addConn = (async function () {
                try {

                    const sql = require('mssql')
                    let pool = await sql.connect(config);
                    let results = await pool.request()
                    return await pool.request()
                        .query('insert into dbo.nirvana (FirstName, LastName, AmountDuePrevious, AmountDueCurrent) values (' + "'" + firstName + "'" + ',' + "'" + lastName + "'" + ', 0, 0);');
                    //.query('insert into dbo.nirvana (FirstName, LastName, AmountDuePrevious, AmountDueCurrent) values ('+lastName+' , '+firstName+' , 0, 0);');
                    console.log(results);
                } catch (err) {
                    console.log(err);
                }
            });

 addConn();
        }






    </script>
Post a comment

comment list (0)

  1. No comments so far