With the below lambda function, and api gateway I have I am getting the response but it shows multiple rows with the same data in the api response where as it has only one row in the csv.
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const csv = require('csv-parser');
const bucket = 'myfilesforcsv';
const objectkey = 'csvFileSheet1.csv';
const params = { Bucket: bucket, Key: objectkey };
const results = [];
exports.handler = function (event, ctx, callback) {
try {
const file = s3.getObject(params).createReadStream();
file
.pipe(csv())
.on('data', function (data) {
results.push(data); // --> here
})
.on('end', () => {
console.log(results);
callback(null, results);
});
} catch (err) {
console.log(err);
callback(Error(err));
}
};
API response:
[
{
"Number": "104",
"Name": "Sobhit Sharma",
"Gun": "M416"
},
{
"Number": "104",
"Name": "Sobhit Sharma",
"Gun": "M416"
}
]
Where as excel sheet has only one row:
CodePudding user response:
You've declared the results
array globally, so each call will append to the previous results
.
This will continue for the lifetime of the lambda. Redeployments will appear to "reset" the result as a new instance of the lambda will be instantiated.
Declare the result array inside your handler:
exports.handler = function (event, ctx, callback) {
try {
const results = [];
// ...