I'm using TypeScript and mssql (v4) NodeJS package to connect my application to a SQL Server database. I'm facing that it seems that the code defined for sql.ConnectionPool is not being called and I was not able to figure out the reason. Basically I want to perform an INSERT operation using a prepared statement. Here's my code of CRUD.ts file:
var sql = require('mssql');
var config = {
server: 'localhost',
database: 'MyDB',
user: 'MyUser',
password: '*******',
port: '1433'
};
export async function newItem(someValue1: string, someValue2: string) {
try {
const db = new sql.ConnectionPool(config, err => {
console.info("Checking for errors...");
if (err) {
console.error("Connection failed: " + err);
} else {
console.info("Connected")
const insertStatement = 'INSERT INTO [dbo].[MyData]([SomeValue1],[SomeValue2])VALUES(@SomeValue1, @SomeValue2)';
const ps = new sql.PreparedStatement(db);
ps.input('SomeValue1', sql.String)
ps.input('SomeValue2', sql.String)
ps.prepare(insertStatement, err => {
if (err) {
console.error(err);
} else {
ps.execute({
SomeValue1: someValue1,
SomeValue2: someValue2
}, (err, result) => {
if (err) {
console.error(err);
} else {
ps.unprepare(err => {
if (err) {
console.error(err);
}
});
}
});
}
});
}
});
}catch (e) {
console.error(e);
}
}
I tried following several examples on mssql documentation with no success.
Any kind of help would be really appreciated.
Thanks in advance.
asyncfunction so you really should rewrite your code to useawaitinstead of callbacks. e.g.result = await ps.prepare(insertStatement)and then you can avoid many levels of nested callbacks.