變數 SNS 是一個 JSON 物件。我想從中提取值并將其存盤在另一個變數中。
`const sns = event.Records[0].Sns.Message;`
我想提取Trigger.Namespace,并Trigger.Dimensions.value和Trigger.MetricName來自SNSJSON變數。
const sns_NameSpace = sns.Trigger.Namespace;
const sns_ApiId = sns.Trigger.Dimensions.value;
const sns_MetricName = sns.Trigger.MetricName;
console.log(sns_ApiId '_' sns_MetricName)
console.log(sns_NameSpace)
我收到以下錯誤:
ERROR Invoke Error
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'Namespace' of undefined",
"stack": [
"TypeError: Cannot read property 'Namespace' of undefined",
" at Runtime.exports.handler (/var/task/index.js:7:38)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
console.log(sns) returns the following:
{
"AlarmName": "ZabbixPy 5XX Error - HTTP",
"AlarmDescription": null,
"AWSAccountId": "123456789",
"NewStateValue": "ALARM",
"NewStateReason": "Threshold Crossed: 1 out of the last 1 datapoints [24.0 (01/11/21 11:42:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -> ALARM transition).",
"StateChangeTime": "2021-11-01T11:43:35.422 0000",
"Region": "Asia Pacific (Mumbai)",
"AlarmArn": "arn:aws:cloudwatch:ap-south-1:1234567890:alarm:ZabbixPy 5XX Error - HTTP",
"OldStateValue": "INSUFFICIENT_DATA",
"Trigger": {
"MetricName": "5xx",
"Namespace": "AWS/ApiGateway",
"StatisticType": "Statistic",
"Statistic": "SUM",
"Unit": null,
"Dimensions": [
{
"value": "someid",
"name": "ApiId"
}
],
"Period": 60,
"EvaluationPeriods": 1,
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"Threshold": 1,
"TreatMissingData": "- TreatMissingData: missing",
"EvaluateLowSampleCountPercentile": ""
}
}
Event data when trying to filter out:

Event data without any filters - console.log(sns):

uj5u.com熱心網友回復:
您應該將 JSON 決議為這樣的物件:
const snsObj = JSON.parse(sns);
然后嘗試訪問該屬性:
snsObj.Trigger.Namespace
uj5u.com熱心網友回復:
使用您的console.log(sns)值進行測驗時沒有問題。
我可以查看事件資料嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/343643.html
標籤:javascript 节点.js json aws-lambda 亚马逊-sns
上一篇:反序列化時的映射問題
