简体   繁体   中英

AWS lambda TypeError: Cannot read property 'toString' of undefined

I have a nodejs lambda function in which the code consists toString() function what we generally use to convert something to string value.

when am writing the code directly in the console of AWS lambda its works perfectly,But when i upload the same function using S3 bucket it throws the below error

TypeError: Cannot read property 'toString' of undefined 
at Response.<anonymous> (/var/task/GetAssetMobile3.js:46:40) 
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:355:18) 
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:105:20) 
at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:77:10) 
at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:615:14) 
at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10) 
at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12) 
at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10 
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9) 
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:617:12

Function code:

"use strict"; 

var AWS = require('aws-sdk');
var s3 = new AWS.S3();
var lambda = new AWS.Lambda();
var pg = require('pg');
console.log('Loading function');
exports.handler = function(event, context, callback) {

var userName=event.headers.username;
var passWord=event.headers.password;
var partybranchid ="event.body.FieldValue";
var modifiedon="event.body.ModifiedON";
var RES;
var params = {
        FunctionName: 'ServiceAuthentication', 
        InvocationType: 'RequestResponse',
        LogType: 'Tail',
        Payload: '{ "userName": "'+userName+'","passWord": "'+passWord+'" }'
};
lambda.invoke(params, function(err, data) {
    if (err) {
      context.fail(err);
      console.log("Response is not received error in login invoking function ");
    } else {
      var response=data.Payload;
      var auth=response.replace('"true"','true');
      if(auth === 'true'){
          console.log("authentication is true continue process");
          s3.getObject({
                Bucket: 'awslambdaoregon',
                Key: 'DBURL.txt'
              }, function (err, data) {
                  if(err){
                      console.log('calling s3 DBURL file  failed ');
                  }
              });
          var  connectionString=data.Body.toString();     
          var client = new pg.Client(connectionString);
          client.connect(function(err) {   
            if(err) {
                  console.log(err);
                }else{
            var Query ='select * from masset where partybranchid='+partybranchid+';

            console.log('executing query',Query);
            client.query(Query, function(err, result) {

             if(err){
               console.log('Error in executing Query');
               client.end();
             } else {
   console.log(' executing Query successful');
                 }
             }
            });}
          });

      }else{
           RES = '{"ResponseJSON":{"Body":{"Datalist":{"Authentication":'+response+'}}}}'; 
      }
     callback(null, JSON.parse(RES));
    }
    });
};

In the above code line var connectionString=data.Body.toString(); am converting the received data to string but the toString() is not working. how to overcome this error, Am i missing something to install in the node_module folder?

Do you have the data object that gets passed in when called? To me it looks like Body is not defined, and that is why you can't call toString().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM