0

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

2
  • Don't name it the same? Commented Jun 24, 2021 at 12:14
  • 1
    Name your variable var getConn something else. Commented Jun 24, 2021 at 12:14

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I changed my variable to "conn". Now I am getting the error as "conn.query is not a function"
Well, that's different issue, since your getConn() can return either req.request() or err, so you'd need check for that.
Ya I am getting the error as "failed to connect to DEDC: 1433 - self signed certificate"
1

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();

3 Comments

I changed my code. Now I am getting "conn.query is not a function"
@Saisri I think now it should be a problem in the result returned in getConn function itself. Please log the value before return to see what conn is
Ya it is returning the error as "failed to connect to DEDC: 1433 - self signed certificate"
0

We need to use trustServerCertificate: true in DB configuration i.e in const config

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.