英文:
CosmosDBInput binding of Azure Function is not using path Variable defined in HttpTrigger
问题
我使用 azure-functions-kotlin-archetype 创建了一个 Kotlin 函数。我创建了一个 Http 触发器和一个 cosmos 输入绑定,用于从 Cosmos 中读取数据。我还在查询中指定了 SQL 查询,以获取数据。我想将路径变量传递给 Http 触发器,该变量应该在查询中用作参数。
根据 Microsoft 的文档,我已经相应地定义了路径参数。链接如下:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=java#http-trigger-get-multiple-docs-from-route-data-using-sqlquery-java
但是我得到了空白的响应,也就是没有获取到数据。如果我在 SQL 查询中硬编码参数,我可以成功获取数据。有没有人可以告诉我问题出在哪里。以下是函数代码:
@FunctionName("GetData")
fun run(
@HttpTrigger(
name = "req",
methods = [HttpMethod.GET],
authLevel = AuthorizationLevel.ANONYMOUS,
route = "/api/getItems/{id}/{qnt}"
)
request: HttpRequestMessage<Optional<String>>,
@CosmosDBInput(
name = "cosmosdb",
databaseName = "item-db",
collectionName = "item",
sqlQuery = "SELECT * FROM ITEM n where n.id= {id} "
+ "and n.qnt = {qnt}",
connectionStringSetting = "Cosmos_DB_Connection_String"
)
rs: Array<String>,
context: ExecutionContext
): HttpResponseMessage {
return request
.createResponseBuilder(HttpStatus.OK)
.body(rs)
.build()
}
请注意,我已经将代码中的 HTML 实体编码解码,以便更清楚地呈现内容。
英文:
I have created a function with Kotlin using azure-functions-kotlin-archetype. I have created a Http Trigger and a cosmos input binding to read data from the cosmos. I have mentioned sql query also to fetch the data. I want to pass path variable to the http trigger which should be used as parameter in the query.
As per Microsoft documentation I have defined the path parameter accordingly.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=java#http-trigger-get-multiple-docs-from-route-data-using-sqlquery-java
But I am getting blank response, means no data is fetched. If I hard code the parameter in the Sql query, I able to fetch the data. Can any one please tell me the issue here. The Function Code is as below
@FunctionName("GetData")
fun run(
@HttpTrigger(
name = "req",
methods = [HttpMethod.GET],
authLevel = AuthorizationLevel.ANONYMOUS,
route = "/api/getItems/{id}/{qnt}"
)
request: HttpRequestMessage<Optional<String>>,
@CosmosDBInput(
name = "cosmosdb",
databaseName = "item-db",
collectionName = "item",
sqlQuery = "SELECT * FROM ITEM n where n.id= {id} "
+"and n.qnt = {qnt}",
connectionStringSetting = "Cosmos_DB_Connection_String"
)
rs: Array<String>,
context: ExecutionContext): HttpResponseMessage {
return request
.createResponseBuilder(HttpStatus.OK)
.body(rs)
.build()
}
}
答案1
得分: 0
根据您提供的示例查询:SELECT * FROM ITEM n where n.id= {'ID1'} " +"and n.qnt = {1}
,似乎id
是字符串,而qnt
是一个数字。当我们从route
获取参数时,它会将参数视为字符串。因此,如果您直接在sqlQuery
中使用来自route
的参数,它将选择如下的SQL数据:SELECT * FROM ITEM n where n.id = 'ID1' and n.qnt = '1'
。因此,不会获取任何数据。
您需要从route
中获取参数qnt
作为数字而不是字符串,因此请使用route = "api/getItems/{id}/{qnt:int}"
。
===========================更新=========================
为其他社区提供此更新参考:
在SQL查询中使用StringToNumber()
方法,如sqlQuery = "SELECT * FROM ITEM n where n.id= {id} " + "and n.qnt = StringToNumber({qnt})"
。
英文:
According to the sample query you provided: SELECT * FROM ITEM n where n.id= {'ID1'} " +"and n.qnt = {1}
, it seems the id
is string and qnt
is a number. When we get the parameters from route
, it will treat the parameters as string. So if you use the parameters from route
in sqlQuery
directly, it will select the data like this sql: SELECT * FROM ITEM n where n.id = 'ID1' and n.qnt = '1'
. So no data is fetched.
You need to get the parameter qnt
as a number but not as string from route
, so please use route = "api/getItems/{id}/{qnt:int}"
.
===========================Update=========================
Add this update for other communities reference:
Use the method StringToNumber()
in sql query like sqlQuery = "SELECT * FROM ITEM n where n.id= {id} " + "and n.qnt = StringToNumber({qnt})"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论