1

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

} };

2
  • please add information how do you concatenate string to place tableName in final code. Also add to your code console.log(JSON.stringify(event)); to check what event contains Commented Nov 20, 2018 at 21:54
  • I am passing table name from get request. And I get it in console.log(); even table is present and region is also the same. Commented Nov 20, 2018 at 22:09

1 Answer 1

8

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] = [...]
Sign up to request clarification or add additional context in comments.

4 Comments

THANKS FOR REPLAY, but the problem still exists
TypeError: Cannot set property 'unitest' of undefined at exports.handler (/var/task/index.js:181:32) where tableName='unitest';
I am doing it like var params = {}; var RequestItems = {}; params.RequestItems[tableName] = [ {...} ];
Still an issue with your json. params.RequestItems creates a new attribute for the params object (it's not using the variable you defined)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.