Dhaval Chheda
Dhaval Chheda

Reputation: 5187

errorMessage: "event is not defined" in lambda function nodejs

I am trying to run a lambda function attached to an API gateway GET request and below is the code

const AWS = require('aws-sdk');
const s3 = new AWS.S3();

const bucketName = "dhaval-upload";

let params = {
        Bucket: bucketName, 
        Key: event.fileName
};

exports.handler = async (event, context, callback) => {
    return await s3.getObject(params).promise()
    .then((res) => {
        return "abcd";
        // return res.Body.toString('utf-8');
    })
    .catch((err) => {
        return err;
    });
};

but I am getting the below error

errorMessage: "event is not defined"
errorType: "ReferenceError"

But I don't understand the reason for this as I have another POST request running perfectly..

Any help will be highly appreciated

Upvotes: 2

Views: 4830

Answers (1)

Carlos Jafet Neto
Carlos Jafet Neto

Reputation: 891

You need to place params inside your handler, like this:

exports.handler = async (event, context, callback) => {

    let params = {
        Bucket: bucketName, 
        Key: event.fileName
    };

    return await s3.getObject(params).promise()
    .then((res) => {
        return "abcd";
        // return res.Body.toString('utf-8');
    })
    .catch((err) => {
        return err;
    });
};

Upvotes: 2

Related Questions