7

I'm trying to achieve something that I assumed would be pretty standard, but I've hit a wall with it. I have a bunch of data coming at me that I want to store in DynamoDB

    const putDataParams = {
        TableName: "sensorNodeData2",
        Item: {
            "deviceId":   {"S": event.deviceId}, 
            "timeStamp":   {"N": event.time},
            "rssi": {"S": event.rssi},
            "seq":  {"S": event.seqNum},
            "payload": {"L": payloadArrayInt16.map(x => x.N)}
        }
    };
    console.log('Data PutItem Params = ' + JSON.stringify(putDataParams));

    dynamodb.putItem(putDataParams, function(err, data) {
        if (err) {
            console.log(err);
            context.fail('ERROR: Dynamo failed: ' + err);
        }
        else {
            console.log(data);
            context.succeed('SUCCESS');
        }
    });

The problem is I cannot for the life of me figure out how to get the list part to work. I've defined it first as :

var payloadArrayInt16=  new Uint16Array(dataArrayMaxIndex); 

and the error is:

"errorMessage": "ERROR: Dynamo failed: InvalidParameterType: Expected params.Item[payload].L to be an Array"

Then I tried :

var payloadArrayInt16= [dataArrayMaxIndex];

Which went through, but obviously doesn't do what I want... when I print out the params, it's not pulled out the contents of the array. It sees:

"Temp":{"L":[null,null,null,null,null,null,null,null,null,null]}

I'm pulling my hair out. There isn't a single example anywhere on how to do this. Can someone please put me out of my misery?!

1 Answer 1

11

DynamoDB array of numeric values is represented like this:

"payload": {
    "L": [
      {
        "N": "1"
      },
      {
        "N": "2"
      },
      {
        "N": "3"
      }
    ]
  }

Now if your input array payloadArrayInt16 is like this:

[1,2,3]

You have to transform it to dynamo db format:

payloadArrayInt16.map(x => { return { "N": x.toString() }});

But you can make your life much easier by using this official aws dymanodb js library It handles all transformations between native js object structures to dynamo db data structures and viceversa.

Also if you are using AWS IoT Core you can write your data to dynamodb directly without any lambda.

Sign up to request clarification or add additional context in comments.

3 Comments

Crumbs. You've hit the nail on the head. Thanks heaps!
And you're totally right. DocumentClient worked perfectly for putting items into the database. The reason I didn't use it was I couldn't get it working for querying and kind of gave up on it... Maybe I'll give it another whirl.
DynamoDB data representation with its {name -> value} AttributeValues: docs.aws.amazon.com/amazondynamodb/latest/APIReference/…

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.