Azure Function: Azure SQL (Input) Binding - Passing a variable as a Parameter [NodeJS]

Matt Handy 5 Reputation points
2024-02-23T02:30:32.0433333+00:00

While writing an Azure Function in NodeJS, I'm trying to pass a variable into the Parameter section of the Integration/Function.json configuration. The only working reference seems to be Query.customVariable where customVariable is a query parameter passed into the function as a get method. I can't reference any post variables, and no documentation or articles reference anything except for "Query." Does anyone know how to pass (reference) a variable from the function into the binding? For example, in the parameter section: @id={testVariable} -- where testVariable is a variable defined in the body of the function?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
{count} vote

1 answer

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,446 Reputation points Moderator
    2024-02-23T16:06:13.88+00:00

    Matt Handy Thanks for posting your question in Microsoft Q&A. Yes, you can pass the part of the body to the binding and refer JSON payloads section which describes more about limitations, performance etc. I shared a sample code/request for V4 in this discussion thread and you can try the following code for V3 (modified from repo: https://github.com/Azure/azure-functions-sql-extension/tree/975630631c435c3c78660a96476e0f90b98b69cf/samples/samples-js/GetProducts) index.js

    module.exports = async function (context, req, products) {
        context.log('JavaScript HTTP trigger function processed a request.');
    
        context.log(JSON.stringify(products));
        return {
            status: 200,
            body: products
        };
    }
    

    function.json

    {
      "bindings": [
        {
          "authLevel": "function",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get",
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        },
        {
          "name": "products",
          "type": "sql",
          "direction": "in",
          "commandText": "select * from Products where Cost = @Cost",
          "commandType": "Text",
          "parameters": "@Cost={cost}",
          "connectionStringSetting": "SqlConnectionString"
        }
      ]
    }
    

    Sample request/response: User's image

    I hope this helps and let me know if any questions.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.