Faizan Shah
Faizan Shah

Reputation: 133

DynamoDB batchwriteItem don't put data to dynamic TableName in Lambda function

I am trying to write data to different tables, tablename is passed through get request.

var tableName = event.tableName.toString();

When I write hardcoded tableName it works fine but when I write variable Name it throws error.

2018-11-20T21:09:31.532Z 928e237c-ed08-11e8-a312-539d290e67fc {"errorMessage":"Requested resource not found","errorType":"ResourceNotFoundException","stackTrace":["Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:48:27)","Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)","Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10)","Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)","Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)","AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)","/var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10","Request. (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)","Request. (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)","Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:115:18)"]} END RequestId: 928e237c-ed08-11e8-a312-539d290e67fc REPORT RequestId: 928e237c-ed08-11e8-a312-539d290e67fc Duration: 124.41 ms Billed Duration: 200 ms Memory Size: 1024 MB Max Memory Used: 33 MB

Here is the code:

var params = {
RequestItems: {
    tableName :  [
   {
     PutRequest: {
        "Item" : {
        "indexNumber":{
            "N": r1index
        },
         "DateandTime" :{
          "S": DnT1  
        } ,
        "roadId" :{
          "N": id
        },
        "x1_axis":{
           "S": x11
        },
        "y1_axis":{
            "S":  y11
        },
        "z1_axis":{
            "S": z11
        },
        "x2_axis":{
           "S": x21
        },
        "y2_axis":{
            "S":  y21
        },
        "z2_axis":{
            "S": z21
        },
        "latitude":{
            "S": lat1
        },
        "longitude":{
            "S":lng1
        }
    }
     }
   },
   {
     PutRequest: {
        "Item" : {
        "indexNumber":{
            "N": r2index
        },
        "DateandTime" :{
          "S": DnT2 
      } ,
        "roadId" :{
          "N": id
        },
        "x1_axis":{
           "S": x12
        },

        "y1_axis":{
            "S":  y12
        },
        "z1_axis":{
            "S": z12
        },
        "x2_axis":{
           "S": x22
        },
        "y2_axis":{
            "S":  y22
        },
        "z2_axis":{
            "S": z22
        },
        "latitude":{
            "S": lat2
        },
        "longitude":{
            "S":lng2
        }
    }
     }
   },
          {
     PutRequest: {
       "Item" : {
        "indexNumber":{
            "N": r3index
        },
        "DateandTime" :{
          "S": DnT3  
        } ,
         "roadId" :{
          "N": id
        },
        "x1_axis":{
           "S": x13
        },
        "y1_axis":{
            "S":  y13
        },
        "z1_axis":{
            "S": z13
        },
        "x2_axis":{
           "S": x23
        },
        "y2_axis":{
            "S":  y23
        },
        "z2_axis":{
            "S": z23
        },
        "latitude":{
            "S": lat3
        },
        "longitude":{
            "S":lng3
        }
    }
     }
   }
]

} };

Upvotes: 1

Views: 2204

Answers (1)

piisexactly3
piisexactly3

Reputation: 779

I think your json is incorrect. When you define your RequestItems object like

RequestItems: { tableName: [...] }

you actually are hard-coding the string "tableName" as the attribute, not a variable called "tableName". This is why there's no table called that.

The es6 solution is to do this:

RequestItems: { [tableName]: [...] }

The old-fashioned solution is to do this:

var RequestItems = {};
RequestItems[tableName] = [...];

See How To Set A JS object property name from a variable for reference

Edit:

For a more complete solution, try:

var params = {};
params.RequestItems = {};
params.RequestItems[tableName] = [...]

Upvotes: 8

Related Questions