Handling optional parameters in Azure function table bindings

Viewed 681

I'm trying to use a filter on an Azure function to look up a row in a table based on an optional route parameter. If the parameter isn't provided, or doesn't match a row, a default row should be returned. This works if I provide a matching value, or a wrong value, I get a row as I expect. If I provide no value at all, I get the following error:

Exception while executing function: Functions.link. Microsoft.Azure.WebJobs.Extensions.Storage: Object reference not set to an instance of an object.

The three key bits (AFAIU) are:

  • httpTrigger route: "link/{ref?}"
  • Table filter: "Email eq '{ref}' or Default eq true"
  • Endpoint URL: https://[subdomain].azurewebsites.net/api/link/(ref)

Is there some way to construct the filter or route so that I get the second clause of the filter when the optional parameter is not provided? Or is there a better way to do this?

My full function.json looks like this:

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "route": "link/{ref?}",
      "direction": "in",
      "name": "req",
      "methods": [
        "get"
      ]
    },
    {
      "name": "emailRule",
      "type": "table",
      "take": "1",
      "filter": "Email eq '{ref}' or Default eq true",
      "tableName": "RedirectRules",
      "connection": "TestStorageConnectionAppSetting",
      "direction": "in"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
1 Answers

Try to use this route "route": "link/{ref=''}", this way you dont have null value but always empty string

Related