python - 当 "id"绑定(bind)点在 cosmosDB 集合中没有相应的项目时,用 python 编写的 Azure 函数有 500

标签 python azure azure-functions azure-cosmosdb

我一直在使用 python 尝试 azure 函数。我已按照 https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2#http-trigger-look-up-id-from-query-string-python 中找到的指南进行操作。这工作正常,我可以使用 ?id=0 查询 enpoint,并得到正确的响应。尽管一旦我将 0 替换为我的 azure 中的 cosmos db 中不存在的 id,它就会抛出 500。相应的 javascript 示例有效。

堆栈跟踪:

[9/17/19 5:53:18 PM] Executed 'Functions.HttpReader' (Failed, Id=23f0d832-5d00-4ffc-a5b9-3e20fed1c105)
[9/17/19 5:53:18 PM] System.Private.CoreLib: Exception while executing function: Functions.HttpReader. System.Private.CoreLib: Result: Failure
[9/17/19 5:53:18 PM] Exception: AttributeError: 'NoneType' object has no attribute 'type'
[9/17/19 5:53:18 PM] Stack:   File "/usr/local/Cellar/azure-functions-core-tools/2.7.1585/workers/python/deps/azure_functions_worker/dispatcher.py", line 294, in _handle__invocation_request
[9/17/19 5:53:18 PM]     pytype=pb_type_info.pytype)
[9/17/19 5:53:18 PM]   File "/usr/local/Cellar/azure-functions-core-tools/2.7.1585/workers/python/deps/azure_functions_worker/bindings/meta.py", line 63, in from_incoming_proto
[9/17/19 5:53:18 PM]     return binding.decode(datum, trigger_metadata=metadata)
[9/17/19 5:53:18 PM]   File "/usr/local/Cellar/azure-functions-core-tools/2.7.1585/workers/python/deps/azure/functions/cosmosdb.py", line 23, in decode
[9/17/19 5:53:18 PM]     data_type = data.type
[9/17/19 5:53:18 PM] .

代码:

import logging
import azure.functions as func


def main(req: func.HttpRequest, doc: func.DocumentList) -> func.HttpResponse:
    logging.warn('Python HTTP trigger function processed a request.')
    if not doc:
        return func.HttpResponse(
            "Not found",
            status_code=404
        )
    else:
        return func.HttpResponse(
            body=doc[0].to_json(),
            status_code=200
        )

函数.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "type": "cosmosDB",
      "name": "doc",
      "databaseName": "<my-cosmos>",
      "collectionName": "<my-col>",
      "connectionStringSetting": "<conn-string>",
      "direction": "in",
      "id": "{Query.id}"
    }
  ]
}

对我来说,这似乎是一个错误,但我觉得我需要进行健全性检查。

所以这是一个错误吗?或者我搞砸了什么?

最佳答案

是的。这看起来像是 Python 库中的一个错误,它会转换为 func.Document。我确认以下代码在 JavaScript 中按预期工作:

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

    if (context.bindings.document == null)
    {
        context.res = {
            statusCode: 400
        }
    }
    else
    {
        context.res = {
            statusCode: 200,
            body: context.bindings.document
        }
    }
};

感谢您的出现。 Opened this issue to track

如果当前想要在 Python 中执行此模式,则必须不使用输入绑定(bind)并使用基于查询参数的 CosmosDB python SDK 获取文档。

关于python - 当 "id"绑定(bind)点在 cosmosDB 集合中没有相应的项目时,用 python 编写的 Azure 函数有 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57979944/

相关文章:

Python Spotify OAuth 流程

python - 如何在 Python 3 中扩展 datetime.date?

c# - 访问特定文件位置(c :/folder/file) in an Azure website

azure - 共享访问签名 Azure

azure - 如何使用 Azure 资源管理器模板部署 Azure Function(及其代码)?

azure - 如果 Azure Function 无法处理消息,是否可以将 Azure 事件中心配置为保留消息?

azure - Entity Framework : adding an entity to Microsoft. EntityFrameworkCore.DbContext 作为 Azure 函数应用程序中的 DbSet

python - 创建一个描述其他列中缺失值的列

python - Python import 是否将所有代码复制到文件中

build - 利用 EC2 或 Azure 扩展分布式 C# 构建系统?