我有一个nodejs lambda函数,其中的代码包含toString()函数,我们通常用它来将某些东西转换为字符串值。
当我直接在AWS lambda的控制台中编写代码时,它的工作正常,但是当我使用S3存储桶上传相同的功能时,它会抛出以下错误
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
功能代码:
"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));
}
});
};
在上面的代码行中 var connectionString = data.Body.toString(); 我将接收的数据转换为字符串但是toString()不起作用。 如何克服这个错误,我错过了在node_module文件夹中安装的东西吗?
答案 0 :(得分:0)
您是否拥有在调用时传入的数据对象?对我来说,它似乎没有定义Body,这就是为什么你不能调用toString()。