sqlQuery与Azure函数2.x中的查询字符串?

时间:2018-12-11 17:26:38

标签: azure azure-functions azure-functions-core-tools

我在Function 1.x中有一个应用程序,该应用程序从查询字符串获取objId,查询CosmosDb(输入绑定),然后完成其余工作。但是,我将其迁移到v2中,它无法像以前那样工作:仅当在路由模板中提供objId时才查询db,但如果将objId作为查询字符串提供时则不查询(在v1中,即使(如果作为查询字符串提供)。即使我的function.json几乎相同。您能告诉我出什么问题了,如何通过查询字符串使输入绑定进行sql查询?

以下是我的function.json的内容:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "cosmosDB",
      "name": "obj",
      "databaseName": "mydb",
      "collectionName": "objects",
      "sqlQuery": "SELECT * FROM c where c.id = {objId}",
      "connectionStringSetting": "mydbstring",
      "direction": "in"
    }
  ],
  "disabled": false
}

以下是我的功能代码

const client = new CosmosClient({
  endpoint: endpoint,
  auth: {
    masterKey: masterKey
  }
});

module.exports = async function (context, req, obj) {
}

以下是我的proxies.json的内容:

{
    "$schema": "http://json.schemastore.org/proxies",
    "proxies": {
        "Set Object state Id Proxy": {
            "matchCondition": {
                "route": "/obj/{objId}/state",
                "methods": [
                    "GET"
                ]
            },
            "backendUri": "http://localhost:7071/api/setobjstate?objId={objId}"

        }
}

它是这样工作的:

http://localhost:7071/games/bbbbb

但是我如何使它如此工作?

http://localhost:7071/games?objId=bbbbbbbbb

1 个答案:

答案 0 :(得分:1)

根据docs,与路由数据不同,查询值似乎无法传递到sql查询。

https://docs.microsoft.com/en-in/azure/azure-functions/functions-bindings-cosmosdb-v2#http-trigger-get-multiple-docs-using-sqlquery-c

从上面的链接: “ HTTP触发器,使用SqlQuery(C#)从路由数据中查找ID 下面的示例显示一个检索单个文档的C#函数。该功能由使用路由数据指定要查找的ID的HTTP请求触发。该ID用于从指定的数据库和集合中检索ToDoItem文档

该示例说明如何在SqlQuery参数中使用绑定表达式。 您可以将路由数据传递到SqlQuery参数,如图所示,但当前您不能传递查询字符串值。

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

但是,如果您只需要按ID检索文档,就可以在function.json中找到它 用以下命令替换“ sqlQuery”:“ SELECT * FROM c where c.id = {objId}” “ Id”:“ {Query.id}”