1
votes

This is part of my code in lambda to insert some values into an oracle database (from Amplify cli)

exports.handler = async (event, context, callback) => {
     console.log(`EVENT: ${JSON.stringify(event)}`);

       let conn;
       try {
         conn = await oracledb.getConnection(connAttr);
let result = await conn.execute(
           "INSERT INTO employee VALUES (:name, :id, TO_TIMESTAMP(:date1, 'DD-MM-YYYY HH24:MI:SS'), TO_TIMESTAMP(:date2, 'DD-MM-YYYY HH24:MI:SS'))",
              [req.body.name, req.body.discount, req.body.date1, req.body.date2],
              {
                name       : req.body.name,
                id :        req.body.id,
                date1     :  req.body.date1,
                date2        :  req.body.date2

              }
         );
);
         const response = {
           statusCode: 200,
           body: JSON.stringify(result),
           headers: {
             "Access-Control-Allow-Origin": "*",
             "Access-Control-Allow-Headers": "*",
             "Access-Control-Allow-Methods": "*",
             "Access-Control-Allow-Credentials" : "true",
             "content-type": "application/json"
         }
         };
         callback(null, response);

         console.log('Query executed');
       }
         catch (err) {
           const response = {
             statusCode: 400,
           body: JSON.stringify({message: err}),
           headers: {
             "Access-Control-Allow-Origin": "*",
             "Access-Control-Allow-Headers": "*",
             "Access-Control-Allow-Methods": "*",
             "Access-Control-Allow-Credentials" : "true",
             "content-type": "application/json"
         }
           };

         }

        }

When I call the api connected, I get no response so I decided to use the aws console for some quick debugging and the code editor is giving me the warning: req is not defined; please fix or add /*globalreq*/

How can I fix this?

1
You have not set 'req' as the parameter that the function accepts - yossi katz
I'm not sure how the syntax would be for that. getconnection() takes the connection parameters only right? - user25724
You're trying to reference a variable named req. There is no such variable defined anywhere in your code. You probably need to be pulling those values out of event. You're going through the effort of logging the event object, but you aren't doing anything with it at all. - Mark B

1 Answers

0
votes

Seem like you were trying to extract the request body when this Lambda function was called, but you have not assigned that body to your "req" variable. You should explicitly extract it from the event argument, you can refer that body (payload) from the document below https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html