Vipendra Singh
Vipendra Singh

Reputation: 859

How to return results from the synhcronous Azure Function in Nodejs

How do we return data in Azure sync function app. Here is the code I have written to connect with azure key vault and capture secrets but it returns null result. And if I don't use context.done() then it goes for infinity run.

module.exports = function (context, req) {
    var responseMessage = "starting Execution"
    //Capturing the Credentials from the Azure Key vault for the snowflake
    const { DefaultAzureCredential } = require("@azure/identity");
    const { SecretClient } = require("@azure/keyvault-secrets");
    const keyVaultName = "someKVname";
    const KVUri = "https://" + keyVaultName + ".vault.azure.net";
    const credential = new DefaultAzureCredential();
    const client = new SecretClient(KVUri, credential);

    let snowflake_username = async function(client){
        const usr_retrievedSecret = await client.getSecret("username"); 
        return usr_retrievedSecret.value;
    };


    var snf_user = snowflake_username();
    context.log(snf_user);
    context.done();
    return snf_user;
}

Any Idea how can return the username?

Upvotes: 1

Views: 222

Answers (1)

suziki
suziki

Reputation: 14088

You can try to do put snf_user in the response body instead of return like this:

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    //..........

    function snowflake_username(){ 

        //..............

        return 111;
    }
    var snf_user = snowflake_username();
    context.log(snf_user);
    context.res = {
        body: snf_user
    };
    context.done();
};

And then you can get the snf_user from the response body.:)

Upvotes: 1

Related Questions