简体   繁体   中英

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

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"
}

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

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