Reputation: 545
I have a file index.js
as below. Where I am trying to call a async function getConn
in other function createThumbnails
. But I am getting the error as "failed to connect to DEDC: 1433 - self signed certificate"
in the catch block.
const sharp = require('sharp');
const sql = require('mssql')
// CONNECTION CONFIGURATION OF BASE DB
async function getConn() {
try {
const config = {
user: 'sa_user',
password: '*******',
server: 'DEDC',
database: 'DEMO_BASE'
}
const pool = await new sql.ConnectionPool(config)
const req=await pool.connect()
const conn = await req.request()
return conn;
} catch (err) {
return err;
}
};
const createThumbnails = async() => {
try{
var conn = await getConn();
const query = `exec DBBASE.get_client_info`
var clientusers = await conn.query(query);
} catch (err) {
return err;
}
}
createThumbnails()
How do I exactly call the function getConn
inside createThumbnails
. Please help. Thanks in advance
Upvotes: 0
Views: 512
Reputation: 545
We need to use trustServerCertificate: true
in DB configuration i.e in const config
Upvotes: 0
Reputation: 14871
You encounter what called hoisting
. Kyle Simpson has a great explaination on this topic
var getConn = await getConn();
which means getConn
will be initialized first, before assignment, which equivalents to
var getConn // initialized
getConn = await getConn() // assignment
Then turned out that you got the error
Solution here is to store it in a different variable name, like
var conn = await getConn();
async function getConn() {
return {
query: async () => {
console.log("query called");
},
};
}
const createThumbnails = async () => {
try {
var conn = await getConn();
const query = `exec DBBASE.get_client_info`;
var clientusers = await conn.query(query);
} catch (err) {
console.log(err);
}
};
createThumbnails();
Upvotes: 1
Reputation: 10201
It's because you are using variable with the same name as the function.
Try different name:
var conn = await getConn();
const query = `exec DBBASE.get_client_info`
var clientusers = await conn.query(query);
Upvotes: 1