0

I am developing a API with AWS. I am using Claudia API Builder.

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

const createOrder = async (order) => {
    if(!order || !order.id || !order.address ) 
    throw new Error ('To order a pizza you must send a id and the adress of the customer')

    return await docClient.put({
        TableName: 'pizza-order',
        Item: {
            orderId : order.id,
            pizza: order.pizza,
            address: order.address,
            status: 'pending',
        }
    })
}

module.exports = createOrder;

Then I send request using postman

{
    "pizza": 1,
    "address": "Bangladesh",
    "id": 2
}

But It return a Error like THis:

{ "errorMessage": "Response contains a circular reference and cannot be serialized to JSON" }

Any solution!?

1 Answers1

3

You must add the .promise method:

return await docClient.put({
    TableName: 'pizza-order',
    Item: {
        orderId : order.id,
        pizza: order.pizza,
        address: order.address,
        status: 'pending',
    }
}).promise().   <== here
Tobin
  • 1,698
  • 15
  • 24
  • Thanx for the solution. I am reading a book on aws which is using claudia API builder. But I find SAM more useful. What's your thought on moving to SAM and also sorry for the theoretical comments. – Asaduzzaman Himel Apr 27 '21 at 13:53
  • This was also helpful for the SQS module. let response = await clientQueue.getQueueAttributes(params, function (err, data) { if (err) { console.error(err) } else { console.log(data) } }).promise() – nolanjacobson May 09 '22 at 19:37