Azure-Functions icon indicating copy to clipboard operation
Azure-Functions copied to clipboard

"Can't figure out which ctor to call." error with CosmosDB trigger

Open icanfathom opened this issue 1 year ago • 12 comments

I'm trying to run a new Function locally. It uses the CosmosDB trigger. Building works fine, but when trying to run with func host start, I get this error: The 'companiesWrite' function is in error: Unable to configure binding 'cosmosDBTrigger1' of type 'cosmosDBTrigger'. This may indicate invalid function.json properties. Can't figure out which ctor to call.

I see a similar issue here: https://github.com/Azure/Azure-Functions/issues/277. The problem was a misnamed configuration property. But I checked all of mine against both the TypeScript def and the online documentation.

My code:

import { app, InvocationContext } from "@azure/functions";

export async function companiesWrite(documents: any[], context: InvocationContext): Promise<void> {
    context.log(`Cosmos DB function processed ${documents.length} documents`);
}

app.cosmosDB('companiesWrite', {
    connectionStringSetting: 'devworksafetyindex_DOCUMENTDB',
    databaseName: 'wsi',
    collectionName: 'companies',
    createLeaseCollectionIfNotExists: true,
    maxItemsPerInvocation: 40,
    handler: companiesWrite
});

My host.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  }
}

I tried adding an HttpTrigger Function, and this worked fine. So it appears to be something specific to my CosmosDB trigger.

Thanks!

icanfathom avatar Feb 02 '24 22:02 icanfathom

thanks for reporting please share the repro steps.

bhagyshricompany avatar Feb 05 '24 06:02 bhagyshricompany

have you properly used local setting.json file like azurewebjob storage etc.

bhagyshricompany avatar Feb 05 '24 09:02 bhagyshricompany

Yep, local.settings.json is properly configured. I used the wizard to walk through the process.

Reproduction steps (in VS Code):

  1. Ctrl + Shift + P > Azure Functions: Create Function...
  2. Azure Cosmos DB trigger
  3. Provide name
  4. Create new local app settings
  5. Enter DB name
  6. Enter container name
  7. Automatically create lease collection if not exists (true)

The Azure Functions extension does the rest. I did check both my local.settings.json file and my configuration params, and it all seems correct.

icanfathom avatar Feb 06 '24 00:02 icanfathom

@bhagyshricompany See comment above.

icanfathom avatar Feb 09 '24 18:02 icanfathom

I'm facing the same issue. Creating a brand new Azure Function CosmosDB trigger via the VS Code extension generates this error on function start.

sebpelletier avatar Feb 15 '24 19:02 sebpelletier

Same issue, via VS Code

LuloDev avatar Mar 05 '24 00:03 LuloDev

it seems that you need to add 'connection' and 'containerName' instead of connectionStringSetting and collectionName, if you dont the Options probably get recognized as v3Options instead of v4options.

mattonecz avatar Mar 07 '24 10:03 mattonecz

it seems that you need to add 'connection' and 'containerName' instead of connectionStringSetting and collectionName, if you dont the Options probably get recognized as v3Options instead of v4options.

@mattonecz is right, VSCode template is just using the wrong key names. For working V4 names, check Configuration

nagaozen avatar Mar 08 '24 02:03 nagaozen

Nice. This got me past my error.

I guess the request here then is to update the VS Code template.

It would also be nice if the Model version was more obvious somehow. Just looking through my config, I'm not sure how I'd know that I should be using Model v4 over v3.

icanfathom avatar Mar 18 '24 20:03 icanfathom

This issue still persists with the latest Azure Functions Core Tools updated in VisualStudio Code Insiders, and following all the default prompts to create an Azure Cosmos DB Trigger function. using JavaScript, Model V4. The template still creates with Incorrect CosmosDBv3TriggerOptions, and not the correct CosmosDBv4TriggerOptions Thus I need to Edit connectionStringSetting: collectionName: createLeaseCollectionIfNotExists: to the V4 names: connection: containerName: createLeaseContainerIfNotExists:

This has fortunately been pointed out above, and saved me lots of time. This is so unfortunate, as otherwise fantastic to use.

wreyford avatar Apr 24 '24 08:04 wreyford