0
votes

I made todo application, i could process GET,POST method in lambda function but i got error when invoke delete method.Here i want to delete data in dynamo db by making delete query from axios through lambda function

This is axios delete function,it send {"data": {"id":this.id}} to lambda

   axios.delete('https://94sc9th9bi.execute-api.ap-northeast-1.amazonaws.com/prod/item',
        { "data": {"id":this.id}}).then(
          res => {
            console.log(res.data.id)
          }).catch(err => {console.log(err)})

      this.getalltask()
    },

I have lambda api for delete

const AWS = require('aws-sdk')
const docClient = new AWS.DynamoDB.DocumentClient()

exports.handler = async (event) => {
    console.log(event)
    let body = JSON.parse(event.body);
     const scanItemPayload = {
        TableName: 'aws-training',
        Key:{
         id: body.data.id
    }
     }
     console.log(body);

   
    const dynamoDBResponse = await docClient.delete(scanItemPayload).promise()
  console.log(dynamoDBResponse)

    const response = {
        body: JSON.stringify(dynamoDBResponse),
       statusCode: 200,
  headers: {
    "Access-Control-Allow-Origin" : "*", // Required for CORS support to work
    "Access-Control-Allow-Credentials" : true, // Required for cookies, authorization headers with HTTPS 
 
  },
    };
    return response;
};

i test lambda above with

{
    "body": "{\"data\":{\"id\":\"1633613467228\"}}"
}

and i got statusCode 200 and no error and i check that the data is deleted in dynamo db

i have a DELETE method API that is related to the lambda function above, and i test delete method api above by giving query {item} => id=1633613467228 , this is the id i want to delete

but it gave me result

{
  "message": "Internal server error"
}


with error log

Execution log for request f83e7e01-52ca-498d-b3e6-34d972510ad8
Fri Oct 08 15:50:00 UTC 2021 : Starting execution for request: f83e7e01-52ca-498d-b3e6-34d972510ad8
Fri Oct 08 15:50:00 UTC 2021 : HTTP Method: DELETE, Resource Path: /item
Fri Oct 08 15:50:00 UTC 2021 : Method request path: {}
Fri Oct 08 15:50:00 UTC 2021 : Method request query string: {id=1633613467228}
Fri Oct 08 15:50:00 UTC 2021 : Method request headers: {}
Fri Oct 08 15:50:00 UTC 2021 : Method request body before transformations: 
Fri Oct 08 15:50:00 UTC 2021 : Endpoint request URI: https://lambda.ap-northeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:184371581740:function:aws-training-20211006-p-delete/invocations

and i test with postman

https://94sc9th9bi.execute-api.ap-northeast-1.amazonaws.com/prod/item?id=1633613467228

i got error, it seem that the query ?id=1633613467228 is not work

i also test the application , i got CORRS/network error in console although i already set access control orign to *

Access to XMLHttpRequest at 'https://94sc9th9bi.execute-api.ap-northeast-1.amazonaws.com/prod/item?' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
index.vue?0f48:64 Error: Network Error
    at createError (createError.js?2d83:16)
    at XMLHttpRequest.handleError (xhr.js?b50d:117)
xhr.js?b50d:210 DELETE https://94sc9th9bi.execute-api.ap-northeast-1.amazonaws.com/prod/item? net::ERR_FAILED 502

so my questions are:
1.why delete method in api above result in internal server error and how can i test the method. i also got confused of the different between test in api gateway and test in lambda function. Is my test data wrong format?

  1. in axios i have "data": {"id":this.id}}) , is Key:{id: body.data.id} right way to get the data send by axios in aws lambda function?