0
votes

I have a really simple Azure Function with the sole purpose of taking all messages included in a blob and put those messages on a storage queue.

I'm running Functions 2.x, the function is written in JavaScript and I've registered a blob trigger and an output binding for storage queues.

The problem I'm having is that the output binding isn't available in ctx.bindings in my function. I'm using a named output binding, because I will have multiple output bindings. When I change the output binding name to $return and return my data, the messages are written to the queue as expected, but when I set a different name the binding doesn't show up in the context. I can however see the binding definition in ctx.bindingDefinitions.

I'm running the code locally with Azure Function Host, with the proper extensions installed.

My code goes like this:

import { Context } from '@azure/functions'

export async function run(ctx: Context , content: string): Promise<void> {
  try {
    const data = JSON.parse(content)

    if (!ctx.bindings[data.queue]) {
      throw new Error(`No output binding defined for queue '${data.queue}'`)
    }

    ctx.bindings[data.queue] = [...data.messages]
  } catch (e) {
    return Promise.reject(e)
  }
}

And my function.json:

{
  "disabled": false,
  "bindings": [
   {
    "name": "content",
    "type": "blobTrigger",
    "direction": "in",
    "path": "message-batches/{filename}.txt"
   },
   {
      "type": "queue",
      "direction": "out",
      "name": "message",
      "queueName": "message",
      "connection": "AZURE_STORAGE_CONNECTION_STRING"
    }
  ],
  "scriptFile": "index.js"
 }

My incoming content binding is available as ctx.bindings.content. I'm thinking I might be missing something trivial here, but what could be the reason for the binding not to show up under ctx.bindings?

1

1 Answers

2
votes

The output binding is not available in Context.bindings until it's populated with content at runtime.

If we want to check the existence of output definition, turn to Context.bindingDefinitions.

let flag:boolean = false;

for (let bindingDefinition of ctx.bindingDefinitions) {
    if(bindingDefinition.name == data.queue) {
        flag = true;
        break;
    }
}

if(!flag){
    throw new Error(`No output binding defined for queue '${data.queue}'`)
}