Home > Net >  No results when scanning DynamoDB with AWS Lambda(node.js)
No results when scanning DynamoDB with AWS Lambda(node.js)

Time:11-17

I've been attempting to scan the table so I can have more functionality with the data. I'm able to .get from the table successfully, but I can't seem to get the scanning function right.

Sample Table:

controlID(N) controlFunction(S)
1 Protect
2 Assess
3 Protect

Code:

const AWS = require("aws-sdk");
const dynamo = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event, context) => {
    let controlInfo;
    let body;
    let statusCode = 200;
    const headers = {
        "Content-Type": "application/json"
    };
    try {
        controlInfo = await dynamo
        .scan({
            FilterExpression: "controlFunction = :cF",  
            ExpressionAttributeValues: {
             ":cF": { N: "5" }
            },
            ProjectionExpression: "controlID",
            TableName: "testControls",
        })
        .promise();
    } catch (err) {
        statusCode = 400;
        controlInfo = err.message;
      } finally {
        //controlInfo = JSON.stringify(controlInfo);
      }
      body = {
        "Control Info" : controlInfo,
        "Threat Info" : "placeHolder"
      };
      body = JSON.stringify(body);    
    return {
        statusCode,
        body,
        headers
      };
    };`

I was expecting the output to be the items of the table with the specified "controlFunction".

Here are the results I get from running the current script:

{
    "Control Info": {
        "Items": [],
        "Count": 0,
        "ScannedCount": 115
    },
    "Threat Info": "placeHolder"
}

CodePudding user response:

You're using the DocumentClient, which auto-marshalls attribute values on both requests to the SDK and responses from the SDK. That means that you don't need to tell the SDK what type each attribute value is. The SDK will automatically map types between DynamoDB native types and their JavaScript equivalents.

So, instead of:

":attrname": { type: value }

You should use:

":attrname": value

For example:

":cF": 5
  • Related