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.
function.json
– Mikhail Shilkov