这个错误提示表示我们尝试访问一个数组中不存在的元素,通常是数组下标越界。解决方法是确保我们的数组下标不会超出范围,可以在使用数组时进行校验。
示例代码:
resource vmNic 'Microsoft.Network/networkInterfaces@2020-06-01' = {
name: 'myNic0'
type: 'Microsoft.Network/networkInterfaces'
location: resourceGroup().location
properties: {
ipConfigurations: [
{
name: 'ipconfig0'
properties: {
subnet: {
id: subnetRef.id
}
privateIPAllocationMethod: 'Dynamic'
publicIPAddress: {
id: publicIp.id
}
}
}
]
}
}
// 访问一个不存在的元素会报错
output subnetName 'string' = vmNic.properties.ipConfigurations[1].properties.subnet.name
// 可以在访问前进行校验
output subnetName 'string' = if(length(vmNic.properties.ipConfigurations) > 0, vmNic.properties.ipConfigurations[0].properties.subnet.name, 'none')