最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node js can't process sns event message content - Stack Overflow

programmeradmin1浏览0评论

I am trying to get content from Message in SNS event in node js lambda project

here is a code for processing message

exports.handler = (event, context, callback) => {

var message = event.Records[0].Sns.Message;
console.log('Message received from SNS:', message); 

message.Events.forEach(element => {
    console.log(element);

   });
};

sample event:

{ 
 "Records": 
  [ 
    { 
     "EventSource": "aws:sns",
     "EventVersion": "1.0",
     "EventSubscriptionArn": "",
     "Sns": 
       { 
         "Type": "Notification",
         "MessageId": "bc86f105-c320",
         "TopicArn": "arn:aws:sns:ewerwrewrw",
         "Subject": "dwqweq23234",
         "Message": 
           {
           "Events":
             [
               {"EventTimestamp":"2018-03-16T10:51:22Z"},
               {"EventTimestamp":"2018-03-16T10:51:22Z"}
             ],
            "EventDocVersion":"2014-08-15"
           },
          "Timestamp": "2018-03-16T10:51:22.691Z",
          "SignatureVersion": "1",
          "Signature": "",
          "SigningCertUrl": "",
          "UnsubscribeUrl": "",
          "MessageAttributes": {} 
      } 
    } 
  ]
 }

This is what I get in CloudWatch logs:

Message received from SNS: { "Events": [ {"EventTimestamp":"2018-03-16T10:51:22Z"}, {"EventTimestamp":"2018-03-16T10:51:22Z"} ], "EventDocVersion":"2014-08-15" }

TypeError: Cannot read property 'forEach' of undefined at exports.handler

Why I am not being able to parse 'Events' inside message object in event?

I am trying to get content from Message in SNS event in node js lambda project

here is a code for processing message

exports.handler = (event, context, callback) => {

var message = event.Records[0].Sns.Message;
console.log('Message received from SNS:', message); 

message.Events.forEach(element => {
    console.log(element);

   });
};

sample event:

{ 
 "Records": 
  [ 
    { 
     "EventSource": "aws:sns",
     "EventVersion": "1.0",
     "EventSubscriptionArn": "",
     "Sns": 
       { 
         "Type": "Notification",
         "MessageId": "bc86f105-c320",
         "TopicArn": "arn:aws:sns:ewerwrewrw",
         "Subject": "dwqweq23234",
         "Message": 
           {
           "Events":
             [
               {"EventTimestamp":"2018-03-16T10:51:22Z"},
               {"EventTimestamp":"2018-03-16T10:51:22Z"}
             ],
            "EventDocVersion":"2014-08-15"
           },
          "Timestamp": "2018-03-16T10:51:22.691Z",
          "SignatureVersion": "1",
          "Signature": "",
          "SigningCertUrl": "",
          "UnsubscribeUrl": "",
          "MessageAttributes": {} 
      } 
    } 
  ]
 }

This is what I get in CloudWatch logs:

Message received from SNS: { "Events": [ {"EventTimestamp":"2018-03-16T10:51:22Z"}, {"EventTimestamp":"2018-03-16T10:51:22Z"} ], "EventDocVersion":"2014-08-15" }

TypeError: Cannot read property 'forEach' of undefined at exports.handler

Why I am not being able to parse 'Events' inside message object in event?

Share edited Mar 23, 2018 at 18:03 Jarred Olson 3,2631 gold badge21 silver badges34 bronze badges asked Mar 16, 2018 at 11:55 user4666065user4666065 10
  • Because message.Events is undefined. – Dan Commented Mar 16, 2018 at 11:56
  • I understood that. but why I can't parse Events inside message. that's my questions – user4666065 Commented Mar 16, 2018 at 12:00
  • Have you tried console logging message to see you have a property called Events? How are you calling your function? – Dan Commented Mar 16, 2018 at 12:07
  • @Dan yes. I did this: var message = event.Records[0].Sns.Message; console.log('Message received from SNS:', message); You can see console.log result in question. it gives: Message received from SNS: { "Events": [ {"EventTimestamp":"2018-03-16T10:51:22Z"}, {"EventTimestamp":"2018-03-16T10:51:22Z"} ], "EventDocVersion":"2014-08-15" } – user4666065 Commented Mar 16, 2018 at 12:09
  • Is not possible, you need to be sure that there aren't any others part of your code assigning value different values to that variable. – Ele Commented Mar 16, 2018 at 12:11
 |  Show 5 more ments

2 Answers 2

Reset to default 7

worked after I fixed to this:

var message = event.Records[0].Sns.Message;

var msgJson = JSON.parse(message);

msgJson["Events"].forEach(element => { .....

try message["Events"].forEach instead of message.Events may work and check weather property exists using.Actually message.Events this should work if you get the same object in console as you have mentioned but you can avoid error at least by checking the property. ----Edit----if(message && message.hasOwnProperty('Events'))

if(message && message.hasOwnProperty('Events')){
   message.Events.forEach(element => {
    console.log(element);

   });
}

I think the message that you are getting is blank first try printing that because I tried below in browser worked properly:

var obj={ 
 "Records": 
  [ 
    { 
     "EventSource": "aws:sns",
     "EventVersion": "1.0",
     "EventSubscriptionArn": "",
     "Sns": 
       { 
         "Type": "Notification",
         "MessageId": "bc86f105-c320",
         "TopicArn": "arn:aws:sns:ewerwrewrw",
         "Subject": "dwqweq23234",
         "Message": 
           {
           "Events":
             [
               {"EventTimestamp":"2018-03-16T10:51:22Z"},
               {"EventTimestamp":"2018-03-16T10:51:22Z"}
             ],
            "EventDocVersion":"2014-08-15"
           },
          "Timestamp": "2018-03-16T10:51:22.691Z",
          "SignatureVersion": "1",
          "Signature": "",
          "SigningCertUrl": "",
          "UnsubscribeUrl": "",
          "MessageAttributes": {} 
      } 
    } 
  ]
 };
var fun1 = function(event, context, callback){

var message = event.Records[0].Sns.Message;
console.log('Message received from SNS:', message); 
console.log("starting")
message["Events"].forEach(element => {
    console.log(element);

   });
};
fun1(obj,'',function(){console.log("uu")})
发布评论

评论列表(0)

  1. 暂无评论