Search code examples
aws-lambdaamazon-dynamodbapi-gateway

DynamoDB Scan Lambda function returns HTTP request or something that I do not understand, but not list of items


I've deployed a Lambda function, which should get list of items with a scan(params, cb) function. In console, I see something different, not the returned list, but something that lookgs like http request body or response?

Can you please explain how to get the list correctly and what do I get?


exports.handler = async (event, context, callback) => {
    console.log('function started')
    let params = {
        TableName: "documents"
    }

    console.log('params get')
    let respond = await db.scan(params, (err, data) => {
        console.log('scan started')
        if (err) console.log(err, err.stack);
        else {
            console.log('else started')

           return data
        }
    })
    console.log('Respons IS: ')
    console.log(respond)
};

The response is a huge huge huge list of something: enter image description here


Solution

  • You are mixing callbacks and async/await ES6 feature.

    I advise you to only use the latter in this case.

    Here is what it would look like :

    const aws = require('aws-sdk');
    
    const db = new aws.DynamoDB.DocumentClient();
    
    exports.handler = async (event, context) => {
        console.log('function started');
        const params = {
            TableName: "documents"
        };
    
        console.log('params get');
        const respond = await db.scan(params).promise();
        console.log('Respons IS: ');
        console.log(respond);
    
        return ...
    };