Node.Js / Electron使用mssql模块将表单数据插入表中

时间:2017-11-09 01:51:58

标签: javascript sql-server node.js

我将以“我是菜鸟”作为序言。

我正在尝试使用javascript将数据插入到我的MSSQL表中。我正在使用mssql模块,我成功地用它来读取数据,所以我知道它有效,或者至少连接工作对我来说是足够的。

所以基本上我有一个包含2个输入名字和姓氏的表单,然后你点击提交,它会将这些名字添加到表单中。我似乎无法实现这一目标。我觉得我错过了文档的某些部分,但我不能在这里找到答案或者在这里找到答案。

违规代码:

<!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>

然后是违规的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>

善待我仍在学习,所以如果这很明显我道歉。

2 个答案:

答案 0 :(得分:2)

我不知道是否为时已晚。但问题出在这里:

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

将其更改为:

let pool = await mssql.connect(config);

答案 1 :(得分:0)

好吧,我似乎确信它能起作用。我需要继续添加const = require ('mssql');一堆,我不能完全理解为什么。我会将完整的底部部分发布到我的代码中,希望将来能够为其他人提出这个问题。

这是修改后的代码,我也花了很多时间搞清楚sql查询,以便它能正确接受我的变量。这是追踪和错误,非常混乱所以我不建议我的特定方法来得出结果。

 <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>