Response contains a circular reference and cannot be serialized to JSON in AWS

237 Views Asked by At

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

There are 1 best solutions below

2
Tobin On BEST ANSWER

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