You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Logic App中For each循环与items()处理多事件失败排查

问题

我正在开发Azure Logic App,用于处理Azure Key Vault的多种事件(即将过期、已过期)通知。每个Key Vault配置的Event Grid都会将事件发送至同一个Logic App。

Event Grid发送的事件为数组格式,我尝试使用For each循环逐个处理事件:触发器为When a HTTP request is received,For each循环输入为triggerBody(),循环内包含“Send an email (V2)”操作,通过items()函数访问事件属性:

Event Type: @{items()?['eventType']}
Key Vault Name: @{items()?['data']?['VaultName']}
Object Type: @{items()?['data']?['ObjectType']}
Object Name: @{items()?['data']?['ObjectName']}
Expiry Date: @{items()?['data']?['EXP']}

但保存Logic App时出现错误:

Failed to save logic app. The template validation failed: 'The template action 'Send_an_email_(V2)' at line '1' and column '1679' is not valid: "The template language function 'items' must have at least one parameter.".'

我查阅文档得知,For each循环内使用items()无需参数,它会自动引用当前循环项。请问我哪里操作有误?如何正确配置Logic App实现多事件的逐个处理?

当前使用的Schema

{
    "schema": {
        "properties": {
            "$schema": {
                "type": "string"
            },
            "properties": {
                "properties": {
                    "data": {
                        "properties": {
                            "properties": {
                                "properties": {
                                    "EXP": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "Id": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "NBF": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "ObjectName": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "ObjectType": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "VaultName": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "Version": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    }
                                },
                                "type": "object"
                            },
                            "required": {
                                "items": {
                                    "type": "string"
                                },
                                "type": "array"
                            },
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "dataVersion": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "eventTime": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "eventType": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "id": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "metadataVersion": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "subject": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "topic": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "type": "object"
            },
            "required": {
                "items": {
                    "type": "string"
                },
                "type": "array"
            },
            "type": {
                "type": "string"
            }
        },
        "type": "object"
    }
}

Event Grid输出示例

[
    {
        "id": "5a",
        "topic": "/subscriptions/8",
        "subject": "kv",
        "eventType": "Microsoft.KeyVault.SecretNearExpiry",
        "data": {
            "Id": "https://kv",
            "VaultName": "kv-v",
            "ObjectType": "Secret",
            "ObjectName": "kv",
            "Version": "",
            "NBF": null,
            "EXP": "2023-06-13T00:00:00Z"
        },
        "dataVersion": "1",
        "metadataVersion": "1",
        "eventTime": "2023-06-12T00:00:00Z"
    }
]

解决方案

错误原因

  1. Schema定义严重错误:当前Schema存在多层冗余嵌套(比如data下嵌套三层properties),完全不符合Event Grid实际输出的数组结构,导致Logic App无法识别请求体是数组类型,进而无法正确关联items()函数的循环上下文。
  2. 循环输入的上下文关联失败:因为Schema解析错误,Logic App无法确认triggerBody()是数组,所以items()函数找不到对应的循环容器,触发参数缺失的报错。

修正步骤

  1. 替换HTTP触发器的Schema
    使用匹配Event Grid实际输出的正确Schema:

    {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                },
                "topic": {
                    "type": "string"
                },
                "subject": {
                    "type": "string"
                },
                "eventType": {
                    "type": "string"
                },
                "data": {
                    "type": "object",
                    "properties": {
                        "Id": {
                            "type": "string"
                        },
                        "VaultName": {
                            "type": "string"
                        },
                        "ObjectType": {
                            "type": "string"
                        },
                        "ObjectName": {
                            "type": "string"
                        },
                        "Version": {
                            "type": "string"
                        },
                        "NBF": {},
                        "EXP": {
                            "type": "string"
                        }
                    }
                },
                "dataVersion": {
                    "type": "string"
                },
                "metadataVersion": {
                    "type": "string"
                },
                "eventTime": {
                    "type": "string"
                }
            }
        }
    }
    
  2. 确认For Each循环配置
    保持For Each循环的输入为triggerBody(),此时Schema已正确识别请求体为数组,Logic App会自动绑定循环上下文,items()函数可正常无参数使用。

  3. 保留邮件内容表达式
    原有的表达式无需修改,修正Schema后路径可正确匹配事件属性:

    Event Type: @{items()?['eventType']}
    Key Vault Name: @{items()?['data']?['VaultName']}
    Object Type: @{items()?['data']?['ObjectType']}
    Object Name: @{items()?['data']?['ObjectName']}
    Expiry Date: @{items()?['data']?['EXP']}
    

验证建议

  • 保存Logic App后,手动发送符合输出示例的POST请求到触发器URL,测试循环是否能逐个处理事件并发送邮件。
  • 若仍有问题,检查For Each循环的设置,确认未误修改循环输入的表达式,或开启并发控制选项(如果需要并行处理)。

内容的提问来源于stack exchange,提问作者Auto geek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 18:43:16