Reputation: 1597
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?!
Upvotes: 7
Views: 12604
Reputation: 1062
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.
Upvotes: 11