user4666065
user4666065

Reputation:

node js can't process sns event message content

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?

Upvotes: 2

Views: 3686

Answers (2)

user4666065
user4666065

Reputation:

worked after I fixed to this:

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

var msgJson = JSON.parse(message);

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

Upvotes: 7

Neha Tawar
Neha Tawar

Reputation: 705

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")})

Upvotes: 0

Related Questions