首页 > 解决方案 > Azure Functions 2.x 中带有查询字符串的 sqlQuery?

问题描述

我在 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

标签: azureazure-functionsazure-functions-core-tools

解决方案


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

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

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

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

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

但是,如果您只需要按 ID 检索文档,您可能会在 function.json 中将 "sqlQuery": "SELECT * FROM c where c.id = {objId}" 替换为 "Id": "{Query. ID}”


推荐阅读