MissingRequiredParameter: Missing required key 'Message' in params

17,822

I found the answer. I had to define my variable too as a string using stringify() command or else JSON formatted message cannot be sent.

Share:
17,822

Related videos on Youtube

Ashish
Author by

Ashish

Updated on June 04, 2022

Comments

  • Ashish
    Ashish almost 2 years

    I'm trying to invoke a code in AWS Lambda. This Lambda code has been configured with my IOT button. On running this code, I don't see any errors. Also,I don't really see the required push notification on my mobile device.

    I can see this message in my console : MissingRequiredParameter: Missing required key 'Message' in params

    This is my code:

    'use strict'; 
    
    console.log('Loading function'); 
    
    var AWS = require('aws-sdk');  
    
    var sns = new AWS.SNS();
    
    AWS.config.region = 'xxxxx'; 
    
    const TopicArn = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'  
    
    exports.handler = function(event, context) { 
    
        console.log("\n\nLoading handler\n\n"); 
        console.log('Received event:', event); 
    
    
      const sin = 
          {
    "default": "Start", 
    "APNS_SANDBOX":"{\"aps\":{\"alert\":\"Start\"}}", 
    "GCM": "{ \"notification\": { \"text\": \"Start\" } }"
    } // for single click
      const doub = {
    "default": "Stop", 
    "APNS_SANDBOX":"{\"aps\":{\"alert\":\"Stop\"}}", 
    "GCM": "{ \"notification\": { \"text\": \"Stop\" } }"
    } // for double click
      const lon = {
    "default": "SOS", 
    "APNS_SANDBOX":"{\"aps\":{\"alert\":\"SOS\"}}", 
    "GCM": "{ \"notification\": { \"text\": \"SOS\" } }"
    } //  for long click
    
      var singleClick = sin[Math.floor(Math.random()*sin.length)]; 
       var doubleClick = doub[Math.floor(Math.random()*doub.length)]; 
       var longClick = lon[Math.floor(Math.random()*lon.length)]; 
    
       var randomMessage = singleClick;
    
        if(event.clickType == "DOUBLE")
        { 
      randomMessage = doubleClick; 
        } 
    
        if(event.clickType == "LONG")
        { 
       randomMessage = longClick; 
        } 
    
    
        sns.publish ({               
        Message: randomMessage, 
        TopicArn: TopicArn 
        }, 
    
        function(err, data) { 
            if (err) { 
                console.log(err.stack); 
                return; 
    
            } 
            console.log('push sent'); 
            console.log(data);
            context.done(null, 'Function Finished!');
            });
            }
    

    Can someone help me debug this error?

    • Michael - sqlbot
      Michael - sqlbot over 6 years
      console.log(randomMessage);. It looks like the variable will be undefined, based on only the code shown here.
    • Ashish
      Ashish over 6 years
      Yeah. I know. I need to trigger my IOT button functions to send messages to my app in Android and iOS. Is there anything, I need to modify in my code?
    • Michael - sqlbot
      Michael - sqlbot over 6 years
      Yes, fix the undefined variable if that is the case.