簡體   English   中英

將一組對象放入 dynamoDB

[英]put an array of object into dynamoDB

我有一個接收此 JSON 的 API:

{
    "name": "Mar/19",
    "month": "3",
    "year": "2019",
    "credit": [
        {
            "name": "Income salary",
            "value": "6500"
        }       
    ],
    "debit": [
        {
            "name": "Rent",
            "value": "2500"
        }       
    ]
}

我的后端在 AWS 上運行,需要將這些信息放在 dynamoDB 表中。 Lambda 函數如下所示:

const params = {
    Item: {
        "id": {
            S: ""+Math.random()
        },
        "name": {
            S: event.name
        },
        "month": {
            N: event.month
        },
        "year": {
            N: event.year
        },           
    },
    TableName: "billing-circle"
};
dynamodb.putItem(params, function(err, data){
    if(err) {
        console.log(err);
        callback(err);
    } else {
        console.log(data);
        callback(null, data);
    }
});

API 在集成請求上聲明了一個映射模板,如下所示:

#set($inputRoot = $input.path('$'))
{
  "name" : "$inputRoot.name",
  "month" : "$inputRoot.month",
  "year" : "$inputRoot.year",
  "credit" : [
        #foreach($elem in $inputRoot.credit)
        {
            "name" : "$elem.name",
            "value" : "$elem.value"
        } 
        #if($foreach.hasNext),#end
    #end
    ],
    "debit" : [
        #foreach($elemDebit in $inputRoot.debit)
        {
            "name" : "$elemDebit.name",
            "value" : "$elemDebit.value",
            "status" : "$elemDebit.status"
        } 
        #if($foreach.hasNext),#end
    #end
    ]
}

我正在嘗試從 Postman 調用 API,但出現此錯誤:

{
    "errorType": "ValidationException",
    "errorMessage": "Supplied AttributeValue is empty, must contain exactly one of the supported datatypes",
    "trace": [
        "ValidationException: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes",
        "    at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27)",
        "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
        "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
        "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)",
        "    at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
        "    at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
        "    at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
        "    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)",
        "    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)",
        "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:116:18)"
    ]
}

有人可以幫我解決這個問題嗎?

我找到了一個解決方案,但我不確定是否是最好的解決方案。 我正在使用 DynamoDB DocumentClient 而不是 DynamoDB 標准庫。 我的 lambda 函數現在是這樣的:

const AWS = require('aws-sdk');
//const dynamodb = new AWS.DynamoDB({region: 'sa-east-1', apiVersion: '2012-08-10'});
const documentClient = new AWS.DynamoDB.DocumentClient();

exports.handler = (event, context, callback) => {

console.log(event);
var input = {
    "id": ""+Math.random(), "name": event.name, "month": event.month, "year": event.year,
    "credit": event.credit,
    "debit": event.debit
};
console.log("input");
console.log(input);
const params = {
    Item: input,
    // Item: {
    //     "id": {
    //         S: ""+Math.random()
    //     },
    //     "name": {
    //         S: event.name
    //     },
    //     "month": {
    //         N: event.month
    //     },
    //     "year": {
    //         N: event.year
    //     },
    //     "credit": {
    //             "M": {
    //                 "name": {
    //                     S: event.credit.name
    //                 },
    //                 "value": {
    //                     N: event.credit.value
    //                 }                        
    //             }
    //         }
    //     "debit": { L: [{
    //         M: {
    //             "name": {
    //                 S: event.debit.name
    //             },
    //             "value": {
    //                 N: event.debit.value
    //             },
    //             "status": {
    //                 S: event.debit.status
    //             }
    //         }
    //     }] }, },
    TableName: "billing-circle"
};
//dynamodb.putItem(params, function(err, data){
documentClient.put(params, function(err, data){
    if(err) {
        console.log(err);
        callback(err);
    } else {
        console.log(data);
        callback(null, data);
    }
});
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM