在AWS-Lambda中运行node.js代码时发生错误
问题描述:
在AWS-Lambda中运行代码时出现以下错误。在AWS-Lambda中运行node.js代码时发生错误
TypeError: first argument must be a string or Buffer at ClientRequest.OutgoingMessage.write (_http_outgoing.js:447:11) at EventEmitter. (/var/task/index.js:52:13) at emitOne (events.js:77:13) at EventEmitter.emit (events.js:169:7) at exports.handler.eventEmitter.on.offset (/var/task/index.js:57:18)
'use strict';
let https = require('https');
exports.handler = (event, context, callback) => {
var ratesData =[];
var totalRecords =0;
var events = require('events');
var options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET'
};
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();
eventEmitter.on('getJson', function(offset)
{
const req = https.request(options.toString(), (res) => {
let body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end',() => {
console.log('Successfully processed HTTPS response');
// If we know it's JSON, parse it
if (res.headers['content-type'] === 'application/json')
{
var requestedJson = JSON.parse(body);
body = requestedJson.records;
totalRecords = requestedJson.total_records;
body.forEach(function(record)
{
ratesData.push(record);
});
}
callback(null, body);
});
});
req.on('error', callback);
req.write(JSON.stringify(event.data));
req.end();
});
console.log('in calling');
// Bind the connection event with the handler
eventEmitter.emit('getJson',0);
for(var i=1;i < (totalRecords/100)+1;i++)
{
eventEmitter.emit('getJson',i);
}
console.log(ratesData);
};
答
对于一个你有toString()
对你的选择对象。删除。
我还建议在本地进行测试,因为它更容易调试。
// filename: test.js - run as node test.js
// Require your lambda
require('./index.js');
// Dummy context
var context = { };
// The event object - make sure of its format. S3 buckets pass a Records array for example
var e = { };
// Call the Lambda function
lambda.handler(e, context, function(err, result) {
console.log('------------');
console.log('Context done');
console.log(' error:', err);
console.log(' result:', result);
});