0
votes

I'm trying to make an Azure Function run with CosmosDB output binding.

The trigger is HTTP, the DB is defined with SQL API.

Here is my code:

[FunctionName("ProcessOrderCosmos")]
public static void Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
    [CosmosDB(
         databaseName: "myDB",
         collectionName: "items",
         ConnectionStringSetting = "CosmosDBConnection")]out Item item,
    ILogger log)
    {
        string requestBody = new StreamReader(req.Body).ReadToEndAsync().Result;  

        item=JsonConvert.DeserializeObject<Item>(requestBody);  
    }

However, the function shows the following error:

PartitionKey extracted from document doesn't match the one specified in the header

What is the problem? Since I'm using the binding, I have no control over the payload sent to the server, and don't have access to the headers.

What am I missing?

2

2 Answers

0
votes

Since you have created the collection with a partition key, you need your Item object to include this field. So if you have chosen let's say id as partition key, your model needs to include the property.

0
votes

So for anyone else stumbling across this problem:

The problem was caused by the partitionKey being a date field. Somewhere along the way the serialization of the date resulted in something Cosmos didn't expect to, and that what caused the problem.

I've recreated the container with different field as a partition key and now everything's good.