22

I was following a walkthrough from microsoft learning on how to add an input binding with CosmosDb for an azure function, however when calling the function it keeps returning internal server error (500 http code).

The configuration of the azure function from function.json is:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "Request",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "Response"
    },
    {
      "name": "bookmark",
      "direction": "in",
      "type": "cosmosDB",
      "databaseName": "func-io-learn-db",
      "collectionName": "Bookmarks",
      "connectionStringSetting": "learn_DOCUMENTDB",
      "id": "{id}",
      "partitionKey": "{id}",
      "sqlQuery": ""
    }
  ]
}

There is a learn_DOCUMENTDB configuration settings in the app service which has a valid connection string to cosmos db instance (was automatically created).

The error log entry says that:

Can't bind CosmosDB to type 'System.String'. Possible causes: 1) Tried binding to 'Microsoft.Azure.Documents.Client.DocumentClient, Microsoft.Azure.DocumentDB.Core, Version=2.9.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' but user type assembly was 'System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e.

Is there something I do wrong?

csg
  • 2,047
  • 2
  • 22
  • 35
  • What language you are using? I have post answer, it works well, but it is based on C# library. – Cindy Pau May 25 '20 at 05:16
  • Have to set the value of partitionkey? On my side, partitionkey is testbowman. – Cindy Pau May 25 '20 at 05:18
  • I have the same issue with the same MS Learn question, i tried classic mode but that seemed to break. I've added feedback for microsoft to fix it with a link to this stackoverflow page, hope they fix it. – Sam Jones Jul 02 '20 at 16:44
  • I am having little similar issue with Node 18 env. Tried other Node env but problem is still same. Here is the error what I am getting. `Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting.` – Gunjan Patel Aug 29 '23 at 10:00

10 Answers10

35

I had the same issue, turns out the new UI generates a different binding than the old one.

New UI:

{
  "name": "bookmark",
  "direction": "in",
  "type": "cosmosDB",
  "databaseName": "func-io-learn-db",
  "collectionName": "Bookmarks",
  "connectionStringSetting": "learn-0088a129-899f-4d18-b4db-5fa74daf1cc3_DOCUMENTDB",
  "id": "{id}",
  "partitionKey": "{id}",
  "sqlQuery": ""
}

Old UI:

{
  "type": "cosmosDB",
  "name": "bookmark",
  "databaseName": "func-io-learn-db",
  "collectionName": "Bookmarks",
  "connectionStringSetting": "learn-0088a129-899f-4d18-b4db-5fa74daf1cc3_DOCUMENTDB",
  "id": "{id}",
  "partitionKey": "{id}",
  "direction": "in"
}

Removing the

"sqlQuery": ""

part from the binding fixed it for me.

You can switch back to the old UI by clicking the "Having issues? Click to go back to the classic Function App management experience" on the app service overview page, as you can see here.

simandibalazs
  • 374
  • 2
  • 2
  • The empty sqlQuery parameter seems to be the problem. I had to go back into the Bindings UI after changing the file. The settings where then blank and after re-entering the settings (rather than discarding the changes) the binding is now working – Silver Solver Jun 06 '20 at 09:21
  • I experienced that adding "{id}" in function.json caused the curly braces not to be saved, causing 404 on all requests. Adding {id} from the Integration section solved the problem. – essi Jul 14 '20 at 12:07
  • 2
    Mine was more perplexing as my "id" parameter wasn't even generated and I had to add it to get to work only to discover that the tutorial didn't mention anything about adding it in the first place! – George Udosen Jan 02 '21 at 01:54
  • in my case {} were missing around id. there's definitely something going wrong – Harish Ninge Gowda Jun 03 '21 at 19:09
  • The next time I created an input trigger it worked without {}. not sure what's going on here. – Harish Ninge Gowda Jun 03 '21 at 19:17
31

For anyone still looking out for this, and if the above solutions don't resolve the issue.

I had the same issue with data not getting retrieved. Checked the function.json as suggested above. The issue though wasn't with the "sqlQuery" parameter.

According to the tutorial on Microsoft Learn, I had set the binding and the partition id values as "id" and "/id", when creating the Input DB binding. This showed up as-is in the function.JSON file. So my JSON file looked like: "id": "id", "partitionKey": "/id"

After looking at the above answers I changed the function.JSON Bindings of the DB to look like:

  "id": "{id}",
  "partitionKey": "{id}"

It was only after this change that it started working.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Chetan
  • 443
  • 4
  • 8
8

Manually updating function.json to this:

 "id": "{id}",
 "partitionKey": "{id}",

worked for me, but as soon as the function was called or saved, it reverted the changes back to:

  "id": "id",
  "partitionKey": "/id",

and stopped working again. The only way I could get it to retain the changes is to edit the Integration section like so: settings for Input Binding Then the settings worked.

Agustis
  • 89
  • 1
  • 2
  • It wasn't saving for me too. I was updating the keys through the editor and through binding menu at the same time, but it would only work for a single request. I had to close all the tabs, update the key through the binding menu, and then it would finally work. – Little Helper May 20 '21 at 19:48
1

I was working trough the exercise that came after the one described here, where an output binding to CosmosDB was required. I was using Javascript (node.js) and could not get it to work even with the above suggestions - until I switched the Function Runtime v3 back to v2. This can be found under your function -> configuration -> function runtime settings.

Arno Peters
  • 675
  • 5
  • 13
  • same, the "sqlQuery": "" doesn't show up in function.json anymore, but I was still getting a 500 error code instead of the 400, it wasn't reaching the function at all when the DB couldn't find a record. Thanks – sickmartian Jul 29 '20 at 14:06
0

Class1.cs:

using System;
using System.Collections.Generic;
using System.Text;

namespace FunctionApp52
{
    public class Class1
    {
        public string Id { get; set; }
    }
}

Function1.cs:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp52
{
    public static class Function1
    {
        [FunctionName("DocByIdFromRouteData")]
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post",
                Route = "todoitems/{partitionKey}/{id}")]HttpRequest req,
            [CosmosDB(
                databaseName: "testbowman",
                collectionName: "testbowman",
                ConnectionStringSetting = "CosmosDBConnection",
                Id = "{id}",
                PartitionKey = "{partitionKey}")] Class1 item,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            log.LogInformation( "Id is: "+item.Id);
            if (item == null)
            {
                log.LogInformation($"ToDo item not found");
            }
            else
            {
                log.LogInformation($"Found ToDo item");
            }
            return new OkObjectResult("!!!!!!!!!!!!!!!!!!");
        }
    }
}

This is the item in cosmos db:

{
    "id": "bowmanid2",
    "PartitionKey": "bowmankey",
    "Description": "bowmandes",
    "testbowman": " "
}

(By the way, you need to set the value of patitionkey. On my side, it is testbowman.)

And then I send a request to http://localhost:7071/api/todoitems/ /bowmanid2

I can get:

enter image description here

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • Looks like I received a quick fix for the issue; thanks any way for your alternative solution! – csg May 29 '20 at 11:10
0

I don't have enough reputation yet to leave a comment instead of an answer, but I had this same problem and the answer from simandibalazs worked for me with one tweak. It seems the new UI also adds a field for "id":"" to the config and if you do not have an id specified there the same error will occur. In my case, I wanted to retrieve a set of items using ONLY the sqlquery, and to do so, I still had to remove the "id":"" line. If you want to retrieve all items you have to remove both of the following lines from the file.

"sqlQuery": ""
"id": ""
Jason
  • 31
  • 6
0

Not enough reputation to comment BUT: I was experiencing some issues with the UI. Both in the bindings blade as in the code+test page. So I went to the app service editor on the function app itself to change these files.

-1

As @arno-peters replied in this question, there is an issue using a javascript environment. However, as he suggests, the runtime v2 is not available anymore (or at least not in sandbox for me). Anyhow, I just changed my environment from Node.js v14 to v12 and now it works using {id}.

MS definitely needs to upgrade its doc...

Hans Araya
  • 345
  • 2
  • 15
-1

In my case, all It needed was to prepend '/' before id in partition key. partitionkey :/id.

Furthermore, Keep SQLQuery empty if not required.

I created a new function in function app. Added input binding and I was getting the same error so I realized something is wrong with binding.

Charlie
  • 4,827
  • 2
  • 31
  • 55
-1

Like a few other people here, I also don't have enough reputation to add a comment. For me to get this working I had to delete "partitionKey" and "collectionThroughput". This was for a Python Function writing to Cosmos DB.

stoicalpirate
  • 250
  • 3
  • 12