Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Azure函数内部添加到Azure CosmosDB_C#_Azure_Azure Cosmosdb_Azure Functions - Fatal编程技术网

C# 从Azure函数内部添加到Azure CosmosDB

C# 从Azure函数内部添加到Azure CosmosDB,c#,azure,azure-cosmosdb,azure-functions,C#,Azure,Azure Cosmosdb,Azure Functions,我创建了一个CosmosDB数据库,其中有一个表,名为MyTable。我的想法是,我想从Azure函数插入到这个表中。环顾四周后,我想到了这个函数: public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out object tableOutput) { log.Info("C# HTTP trigger function processed a request."); //

我创建了一个CosmosDB数据库,其中有一个表,名为MyTable。我的想法是,我想从Azure函数插入到这个表中。环顾四周后,我想到了这个函数:

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);
}
我得到的错误是:

2017-12-07T15:52:44.066执行功能时出现异常: Functions.MyFunc。Microsoft.Azure.WebJobs.Host:处理时出错 函数返回后的参数tableOutput:。 Microsoft.Azure.WebJobs.Extensions.DocumentDB:集合 “tableOutput”(在数据库“mycosmodb”中)不存在。到 自动创建集合,将“CreateIfNotExists”设置为 “真的”。Microsoft.Azure.Documents.Client:消息:{“错误”:[“所有者” 资源不存在“]}

我已经设置了输出参数,并且看到了这里提到的复选框(CreateIfNotExists);然而,我有一个现有的宇宙表设置,并想写信给它;所以我的问题是,如何从Azure函数中访问该表

function.json如下所示:

{
  "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
}
编辑:

有两个人暗示,就这个问题而言,术语表和集合是同义的。如果我理解正确,那么情况似乎并非如此,因为即使我更改function.json中的集合名称以匹配我创建的表的名称,也会出现相同的错误

为了澄清CosmosDB表的配置,我在data explorer中看到了一个名为TableDB的节点,它有我创建的表的一个子节点。

  • 确保这是现有收藏的价值: (您提到MyTable,但它说的是tableOutput)

  • 确保名称相同。集合名称区分大小写。“mytable”和“mytable”是不同的集合


您需要使用正确的“collectionName”值更新Function.json,以便保存新文档。目前,您的Cosmos DB数据库(“mycosmosdb”)中没有名为“tableOutput”的集合

另外,如果希望Azure函数输出绑定在集合不存在时自动创建集合,则将Function.json属性“createIfNotExists”设置为“true”,如果集合不存在,它将创建集合

这两种方法都可以让代码正常工作,但您需要确保“collectionName”设置为已设置的Cosmos DB集合的正确名称

例如,尝试将Function.json更改为以下内容:

{
  "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
}

请分享你的
函数。json
我更新了这个函数。jsonI是CosmosDB的新手,但据我所知,表和集合是不同类型的东西。如果我是正确的,那么我想更新一个表,而不是一个集合(显然,如果我错了,那么这可能就是问题的答案)。@pm_2表,正如您习惯于从SQL数据库中获取的一样,不存在于Cosmos DB中。它们都是Cosmos DB中的文档集合,但如果您使用Table API,那么它基本上只是调用它们的表。因此,要清楚,Azure在其UI中引用表的地方,它意味着一个集合,这就是它所寻找的
{
  "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
}