Azure Function Azure Synapse - Why do I received blank output

Viewed 294

I have successfully connected my DB and run the queries. I am able to see my query result in the terminal. But I still cannot see the response output of the statement body. It is supposed to give me "Unable to establish a connection." if I am not able to connect, "Connected to Database Successfully" if I have successfully execute the query and "Failed to connect to Database" if it does not able to execute the query.

May I request some help on what am I actually missing here? The following are the my function.js and index.js file.

My function.js

  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
} 

My index.js:

var Request = require('tedious').Request;

//const { Connection, req } = require("tedious");
var sql = require("mssql");

// config for your database
var config = {
    user: '<userName>',
    password: '<password>'',
    server: '<SQLDBName>.database.windows.net',
    database: '<DB name',
    port: 1433
};

var connection = new Connection(config);
module.exports =  async function(context) {
    context.log('JavaScript HTTP trigger function processed a request.');

    var conn = new sql.ConnectionPool(config);
    var request = new sql.Request(conn);

    //conn.connect(function (err){
    connection.on('connect', function(err) {
        if (err) {
            context.log(err);

            context.res = {
                status: 500,
                body: "Unable to establish a connection."
            };
            context.done();
        }else {
            exStatement();
        }
    });

    function exStatement() {

        request = new Request.query("SELECT * FROM dbo.dbTable", function (err, recordset) {
            if (err) {
                context.log(err);
                context.res ={
                    status : 400,
                    body : "Failed to connect to Database"
                };
                context.done();
                console.log(err);
            }
            else {
                //queryDatabase();
                context.log(recordset);
                context.res ={
                    status : 200,
                    body : "Connected to Database Successfully",
                };
                context.done();
                console.log(recordset);
            }
            conn.close();
        });
        connection.execSql(request);
    }
};
0 Answers
Related