Accessing a return value within a sql query using connection string

84 Views Asked by At

I am connecting to a database using mssqlnodev8 connection string and I cannot access the result outside the block of code where I make the query. Is there a way for me to return the value that I like outside the query? This is a helper function that I use to transform one value to another using a database.

const transformTable = async (field, value) => {
    try {
        field = field.replace("'","''");
        if (typeof value !== 'boolean' ){
            value = value.replace("'","''");
        }
        const query = `SELECT XXX FROM XXXTable WHERE fieldName = '${field}' AND adpName = '${value}'`;
        //set the variable that I want to return with this function
        let test = '';
        //This is where I make the connection using a connection string
        await sql.query(connectionString, query, (err, info) => {
            if (err){
                console.log(err)
                return
            }
            //I want to set the INFO to the test variable that I created outside of this block 
            test = info
        })
        //I want to return the value of the variable that I set from info
        console.log(test, 'test info')
    } catch (error){
        console.log(error)
    }
}```
0

There are 0 best solutions below