简体   繁体   中英

Returning Output from AWS.DynamoDB.DocumentClient.Scan() Call

I've got a function that returns the number of records from a DynamoDB table (Things):

const table = 'Things';
const region = 'us-east-1';
const profile = 'development';

process.env.AWS_SDK_LOAD_CONFIG = true;
process.env.AWS_PROFILE = profile;

const AWS = require('aws-sdk');
AWS.config.update({ region: region });

function ddb_table_has_records(table_name) {
    const ddb_client = new AWS.DynamoDB.DocumentClient();

    const ddb_query_parameters = {
        TableName: table_name,
        Select: 'COUNT'
    }

    const results = ddb_client.scan(ddb_query_parameters).promise();

    results.then((data) => {
        console.log(data.Count);
        return data;
    }).catch((err) => {
        console.log("Error: ", err);
    })
}

console.log(ddb_table_has_records(table));

When I run this code, I get the following...

PS C:\> node .\get-count-thing.js
undefined
3951

I'm not capturing the data from the scan in the following; although, I see it in the console.log() call:

console.log(ddb_table_has_records(table));

What am I mucking up?

Posting my fix in-case anyone has the same question. I had to make two changes to retrieve the items from the table; I needed to...

  1. ...project ALL_ATTRIBUTES
  2. ...iterate over the collection of Items returned

The following was my function with changes:

function ddb_table_has_records(table_name) {
    const ddb_client = new AWS.DynamoDB.DocumentClient();

    const ddb_query_parameters = {
        TableName: table_name,
        Select: 'ALL_ATTRIBUTES'
    }

    const results = ddb_client.scan(ddb_query_parameters).promise();

    results.then((data) => {
        console.log(data.Count);

        data.Items.forEach((thing) => {
            console.log(thing);
        });

    }).catch((err) => {
        console.log("Error: ", err);
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM