0

I am trying to write a code to connect with snowflake inside the azure function app using nodejs stack. Here is the code I have writtern.

async function af2snf() 
{
console.log("starting connection");
var snowflake = require('snowflake-sdk');
// Create a Connection object that we can use later to connect.
console.log("prep conn");
var connection = snowflake.createConnection( {
    account: "myaccountname",
    username: "myusername",
    password: "mypass",
    database: "dbname",
    schema: "schemaname"
    }
    );
console.log("making connection");

var conn_id = connection.connect( 
        function(err, conn) {
            if (err) {
                console.error('Unable to connect: ' + err.message);
                } 
            else {
                console.log('Successfully connected to Snowflake.');
                // Optional: store the connection ID.
                connection_ID = conn.getId();
                }
            }
        )
return conn_id.getId();
}

console.log("outside fucntion");
console.log(af2snf());
console.log("executed fucntion");

The output I am getting is :

outside fucntion
starting connection
prep conn
making conn
Promise { <pending> }
executed fucntion
Successfully connected to Snowflake.

But What I need is :

outside fucntion
starting connection
prep conn
making conn
Successfully connected to Snowflake.
Promise { <pending> }
executed fucntion

I am very new to nodejs. Please help me out on this

1 Answer 1

1

af2nsf needs to return a promise :

async function af2snf() {
  return new Promise((resolve,reject)=>{
   console.log("starting connection");
   var snowflake = require('snowflake-sdk');

   // Create a Connection object that we can use later to connect.
   console.log("prep conn");
   var connection = snowflake.createConnection( {
    account: "myaccountname",
    username: "myusername",
    password: "mypass",
    database: "dbname",
    schema: "schemaname"
    });
   console.log("making connection");

   connection.connect( async function(err, conn) {
            if (err) {
                console.error('Unable to connect: ' + err.message);
                return reject(err);
            } 
            else {
                console.log('Successfully connected to Snowflake.');
                // Optional: store the connection ID.
                var conn_id = await conn.getId();
                return resolve(conn_id);
             }
      })
  });
}

console.log("outside fucntion");
af2snf().then(conn_id=>{
   console.log(conn_id);
   console.log("executed fucntion");
}).catch(err=>{
   console.error(err);
});
Sign up to request clarification or add additional context in comments.

6 Comments

This is giving me an error SyntaxError: await is only valid in async function... But I have declared function with async keyword
updated. async was needed at connection.connect( async function(err, conn) {
Still giving me the same issue
Please try now.
yeah it helped. Actually I wa trying to replicate the Azure Function in local to trigger the async function. But now since we did some modification in function calling how we can replicate same in Azure function. Any Idea?
|

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.