0

I made one todo app by using lambda. I used sever-less framework for deployment. I made one post request where user can create a todo-list. For testing I am using post-man. For my Lambda function I am using async function. I can able to make post request and my item store in Dynamo db but I got response Internal server error. My goal is show in my postman what I kind of post request I made.

Here is my code:

'use strict'
const AWS = require('aws-sdk');
const uuid = require('uuid');

const dynamoDb = new AWS.DynamoDB.DocumentClient();

module.exports.createTodo = async (event) => {
  const datetime = new Date().toISOString();
  const data = JSON.parse(event.body);
  const params = {
    TableName: 'todos',
    Item: {
      id: uuid.v1(),
      task: data.task,
      done: false,
      createdAt: datetime,
      updatedAt: datetime
    }
  };
  try {
    let data = await dynamoDb.put(params).promise();
    console.log(data);
    return JSON.stringify(data); // this throw me internal server error.
  } catch (error) {
    console.log(error);
  }
};
 

1 Answer 1

1

Since you are calling the Lambda function via API Gateway, you need to convert the response to the following structure -

return {
  statusCode: 200,
  body: JSON.stringify(data),
  // headers: {'Content-Type': 'application/json'}, // Uncomment if needed by your client
}
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry my mistake, I changed the payload and did not deploy it that's why I got the error. But I used your return but now I got empty object. is there any way I can retrieve the post payload,
You are returning data which is the ddb putItem response. Do you want to return that or the function input? Can you add console.log(JSON.stringify(data)) before you return and check what is getting logged?
Sorry, I am new to Lambda. I got what you mean now, I added event body now I am getting: "{\n \"task\": \"nodddnhjbhfyuyttdrdtrsrtydryrddytucddddddfyufyfiyifuyii\"\n}". is there any way I can just display : "task": "nodddnhjbhfyuyttdrdtrsrtydryrddytucddddddfyufyfiyifuyii"
I did this: let response = { statusCode: 200, body: event.body, headers: { 'Content-Type': 'application/json' }, // Uncomment if needed by your client } return response; I got what I wanted but my question is how I just got the body

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.