【问题标题】:azure function http trigger inputting cosmos db document天蓝色函数http触发器输入cosmos db文档
【发布时间】:2018-07-15 02:33:42
【问题描述】:

参考这个azure documentation 还有这个using parameters from http trigger

特指

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

如果我有一个 javascript 函数,一个 http 触发器,它在正文中提供一个 JSON 数据包。如何使用 Azure cosmos db 绑定获取文档,使用绑定将 http json 值传递给 cosmos db 查询?

我希望与此类似:

"sqlQuery": "SELECT * from c where c.departmentId = {departmentId}",

除了{departmentId} 应该是来自httptrigger 的属性(命名为req)?

因此 function.json 看起来像这样:

{
       "authLevel": "function",
       "type": "httpTrigger",
       "direction": "in",
       "name": "req"
     }, {
      "type": "documentDB",
      "name": "inputDocument",
      "databaseName": "mydb",
      "collectionName": "things",
      "partitionKey": "/things/thingid",
      "connection": "my_DOCUMENTDB",
      "direction": "in",
      "sqlQuery": "Select * from things s where s.thingid={httpTrigger_body_thingid}"
    }

javascript 中的 http 触发器在函数中看起来像这样,它可以工作:req.body.thingid,但是绑定到输入会导致错误,“未定义属性”所以我如何使用 HTTP 触发器输入来获取值从一个json数据包到第一个输入中查询cosmos db,都在同一个函数中?

【问题讨论】:

    标签: javascript azure-cosmosdb azure-functions


    【解决方案1】:

    应该只是{thingid}:

    {
      "type": "documentDB",
      "name": "inputDocument",
      "databaseName": "mydb",
      "collectionName": "things",
      "connection": "my_DOCUMENTDB",
      "direction": "in",
      "sqlQuery": "select * from things s where s.thingid={thingid}"
    }
    

    对于类似的 POST 请求

    {
      "thingid": "293a2fc3-799f-4669-92d3-3413f1afa51e"
    }
    

    它将传递context.bindings.inputDocument(javascript数组)中的文档。

    【讨论】:

    • 有帮助,但如果我使用 select * from things s where s.thingid={thingid} 它永远找不到记录。该语句似乎与文档的 id 字段紧密绑定,因为如果我将 {thingid} 替换为文档中“id”的值,那么文档就被定位了。此外,对于第二个示例,“id”:“{thingid}”仅在 id 是键时才有效。我的分区在“thingid”上。
    • 我刚刚测试了两者,它适用于我的收藏。第二个选项确实只适用于id。我会删除它。
    • 是的,所以如果body如下 { "thingid": 1231} ,其中1231不在引号中,它将找不到记录。在引号中工作正常。我会承认失败只是为了继续前进
    猜你喜欢
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 2020-05-14
    • 2018-12-27
    • 2023-01-31
    • 1970-01-01
    相关资源
    最近更新 更多