简体   繁体   English

使用 AWS Lambda(node.js) 扫描 DynamoDB 时没有结果

[英]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.我能够成功地从表中获取,但我似乎无法正确扫描 function。

Sample Table:样品表:

controlID(N)控件ID(N) controlFunction(S)控制函数(S)
1 1个 Protect保护
2 2个 Assess评估
3 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".我期望 output 是具有指定“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.您正在使用 DocumentClient,它自动编组对 SDK 的请求和来自 SDK 的响应的属性值。这意味着您不需要告诉 SDK 每个属性值是什么类型。 The SDK will automatically map types between DynamoDB native types and their JavaScript equivalents. SDK 将自动在 DynamoDB 本机类型及其 JavaScript 等效类型之间生成 map 类型。

So, instead of:所以,而不是:

":attrname": { type: value }

You should use:你应该使用:

":attrname": value

For example:例如:

":cF": 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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