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

I am trying to get content from Message in SNS event in node js lambda projecthere is a code for proces

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745071465a4609574.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信