Reputation: 196
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.
Upvotes: 1
Views: 4848
Reputation: 140
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);
Upvotes: 4
Reputation: 196
OK I seem to have convinced it to work. I needed to keep adding const = require ('mssql');
a bunch which I don't fully comprehend why. I will post the full bottom section to my code in the hope that this question comes 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 recommend 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>
Upvotes: 1