'warning: req is not defined in (nodejs) lambda function

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anh ??c Nguy?n