2
votes

I've created a CosmosDB DB, with a single table, called MyTable. My idea is that I want to insert into this table from an Azure function. Having had a look around, I've come up with this function:

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out object tableOutput)
{
    log.Info("C# HTTP trigger function processed a request.");

    // parse query parameter
    string field1 = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "field1", true) == 0)
        .Value;

    string field2 = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "field2", true) == 0)
        .Value;


    var item = new 
    {    
        field1 = field1,
        field2 = field2
    };

    tableOutput = item;

    return req.CreateResponse(HttpStatusCode.OK);
}

The error that I get is this:

2017-12-07T15:52:44.066 Exception while executing function: Functions.MyFunc. Microsoft.Azure.WebJobs.Host: Error while handling parameter tableOutput after function returned:. Microsoft.Azure.WebJobs.Extensions.DocumentDB: The collection 'tableOutput' (in database 'myCosmosDB') does not exist. To automatically create the collection, set 'CreateIfNotExists' to 'true'. Microsoft.Azure.Documents.Client: Message: {"Errors":["Owner resource does not exist"]}

I have set-up the output parameter, and I have seen the checkbox mentioned here (CreateIfNotExists); however, I have an existing Cosmos table set-up, and would like to write to that; so my question is, how can I access that table from within an Azure function?

The function.json is below:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "documentDB",
      "name": "outputDocument",
      "databaseName": "mycosmosdb",
      "collectionName": "tableOutput",
      "createIfNotExists": false,
      "connection": "mycostmosdb_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}

EDIT:

Two people have implied that for the purposes of this question the terms Table and Collection are synonymous. If I have understood correctly, then this appears to not be the case, as even when I change the collection name in the function.json to match the name of the table that I created, I get the same error.

To clarify the CosmosDB table configuration, I am seeing, inside data explorer, a node entitles TablesDB, which has a sub-node of the table that I've created.

2
Please share your function.jsonMikhail Shilkov
I've updated with question with the function.jsonPaul Michaels
I'm new to CosmosDB, but as I understand it, tables and collections are different types of things. If I'm correct, then I want to update a table, and not a collection (obviously, if I'm wrong then that might be the answer to the question).Paul Michaels
@pm_2 Tables as you're used to from SQL databases do NOT exist in Cosmos DB. They are all Collections of Documents in Cosmos DB, but if you use the Table API then it basically just calls them Tables.Chris Pietschmann
So, to be clear, where Azure refers to a table in it's UI, it means a collection, and that is what it's looking forPaul Michaels

2 Answers

0
votes

You need to update the Function.json with the correct "collectionName" value for your Cosmos DB Collection you're trying to save the new Document to. Currently, you do NOT have a Collection in your Cosmos DB Database ("mycosmosdb") with the name "tableOutput".

Also, if you'd wish for the Azure Function Output binding to automatically create the Collection if it doesn't exist, then set the Function.json property "createIfNotExists" to "true" and it'll create the Collection if it doesn't exist.

Either approach will get your code working without error, but you'll need to make sure the "collectionName" is set to the correct name of your Cosmos DB Collection you have set up.

For example, try changing your Function.json to the following:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "documentDB",
      "name": "outputDocument",
      "databaseName": "mycosmosdb",
      "collectionName": "MyTable",
      "createIfNotExists": true,
      "connection": "mycostmosdb_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}
0
votes
  • Make sure this is the value of your existing collection: (you mention MyTable but it says tableOutput)

    "collectionName": "tableOutput",
    
  • Make sure the names are identical. Collection names are case sensitive. "mytable" and "myTable" are different collections