来自请求正文的Azure Function App HTTP触发器CosmosDB输入查询

时间:2018-12-12 17:01:15

标签: azure azure-functions azure-cosmosdb

我创建了一个Azure函数,该函数通过HTTP POST请求触发。
该请求的正文如下:

{
    "start": '2018-07-25T08:47:16.094Z',
    "end": '2018-07-25T08:47:24.686Z'
}

index.js

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    var inputDocument = context.bindings.inputDocument;
    context.log("INPUT DOCUMENT: " + JSON.stringify(inputDocument));
    context.res = {
            status: 200,
            body: "OK",
        };
};

function.json

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "cosmosDB",
      "name": "inputDocument",
      "databaseName": "Messages",
      "collectionName": "Collection1",
      "sqlQuery": "SELECT * FROM Collection1 c WHERE c.message.timestamp>={start} AND c.message.timestamp<={end}",
      "connectionStringSetting": "cosmosdbaccount_DOCUMENTDB",
      "direction": "in"
    }
  ]
}

我想使用以下两个参数查询CosmosDB实例:

"sqlQuery": "SELECT * FROM Collection1 c WHERE c.message.timestamp>={start} AND c.message.timestamp<={end}",

像我在这里显示的那样进行操作会导致CosmosDB的inputBinding蜂鸣未定义。

2018-12-13T08:19:54.332 [Information] Executing 'Functions.Function1' (Reason='This function was programmatically called via the host APIs.', Id=af8090a4-5fab-4fbd-b26f-a045d8900d9b)
2018-12-13T08:19:56.704 [Information] JavaScript HTTP trigger function processed a request.
2018-12-13T08:19:56.711 [Information] INPUT DOCUMENT: []
2018-12-13T08:19:56.755 [Information] Executed 'Functions.Function1' (Succeeded, Id=af8090a4-5fab-4fbd-b26f-a045d8900d9b)

如果我在CosmosDB数据浏览器中运行相同的确切查询,将startend替换为正确的ISO时间戳,它将返回所有四个文档。

我想念什么吗?我真的没有在网上找到有关此主题的任何信息,所以希望有人已经偶然发现了它。

非常感谢!

1 个答案:

答案 0 :(得分:2)

目前看来空结果是预期的。在CosmosDB绑定中,sqlQuery难以处理嵌套令牌(startend是请求正文的属性),related issue here

这里有三种解决方法。

  1. 只需将startend放在查询字符串中,例如https://functionUrl?start=<timeA>&end=<timeB>

  2. function.json中,设置路由参数startend,即在httpTrigger绑定中添加"route": "{start}/{end}",。然后在网址中放置时间进行访问。

  3. 自行获取请求正文和query CosmosDB