将javascript json / object附加到daynamoDB现有列表" L"通过apigClient和Lambda

时间:2017-05-17 17:15:32

标签: amazon-dynamodb aws-lambda

要附加现有列表的Lambda函数:

    var doc = require('aws-sdk');
    var dynamo = new doc.DynamoDB({
        convertEmptyValues: true
    });

    exports.handler = function(event, context) {
        var params = {
            TableName: 'F_Items',
            Key: { // The primary key of the item (a map of attribute name to AttributeValue)

                "APIuserID": {
                    "N": event.APIuserID
                },
                "f_name": {
                    "S": event.f_name
                },
            },

            UpdateExpression: "SET #bks.#nm = list_append(#bks.#bl, :vals)",
            ExpressionAttributeNames: {
                "#bks": "Books",
                "#bl": "booklist",
            },
            ExpressionAttributeValues: {
                ":vals": event.bVal,
            },
            ReturnValues: 'UPDATED_NEW',
            ReturnConsumedCapacity: 'NONE',
            ReturnItemCollectionMetrics: 'NONE',
        };

        dynamo.updateItem(params, function(err, data) {
            if (err) console.log(err, err.stack); // an error occurred
            else context.succeed(data); // successful response
        });

    } 

通过API Getaway传递低于javascript前端的值;

    var JSitem = {
        "aVal": "a",
        "bVal": "b",
        "nVal": 12
    };
    var apigClient = apigClientFactory.newClient();
    var body = {
        "APIuserID": "1",
        "f_name": "abc",
        "bVal": JSitem
    }
    apigClient.addingbookitemsPut({}, body, {})
        .then(function(result) {
            console.log("passed");
        }).catch(function(result) {
            console.log("err");
        }); 
这里的问题JSitem必须是" L"为了能够将它附加到列表中,我如何构建它,以便 apigClient 将其传递为" L"

以下结构对我没有帮助

    var JSitem = [];

    JSitem["aVal"] = "a";
    JSitem["bVal"] = "b";
    JSitem["nVal"] = 12; 

    var JSitem = [{
            aVal: 'a',
            bVal: 'b',
            nVal: 12
        }];

1 个答案:

答案 0 :(得分:0)

我建议你使用document client而不是dynamodb对象。使用dynamodb对象时,list_append上似乎存在问题。

以下代码包含您在代码中所需的必要更改。

 var doc = require('aws-sdk');
    var dynamo = new doc.DynamoDB({
        convertEmptyValues: true
    });

var docClient = new doc.DynamoDB.DocumentClient();

exports.handler = function(event, context) {

    var params = {
    TableName: 'F_Items',
    Key: {

        "APIuserID":  event.APIuserID,
        "f_name": event.f_name,
    },

    UpdateExpression: "SET #bks.#nm = list_append(#bks.#bl, :vals)",
    ExpressionAttributeNames: {            
        "#bks": "Books",
        "#bl": "booklist",
    },
    ExpressionAttributeValues: {
        ":vals": event.bVal,
    },
    ReturnValues: 'UPDATED_NEW',
    ReturnConsumedCapacity: 'NONE',
    ReturnItemCollectionMetrics: 'NONE',
};

    docClient.update(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else context.succeed(data); // successful response
    });

} 

请求: -

{
    "APIuserID" : 3,
    "f_name" : "name",
    "bVal" : ["somevalue"]
}