Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I'm trying to set up a small api from AWS Lambda to DynamoDB and I am having trouble figuring out if and how I can write an array of objects into a key.

I have an object like

{

    "teamName": "Team Awesome",

    "members": [

        {

            "email": "[email protected]",

            "name": "Bob"

        },

        {

            "email": "[email protected]",

            "name": "Alice"

        }

    ]

The member's array is giving me issues, in the docs, it looks like it can be done considering the list types, but there is just no example how HOW to do it, and I am running out of ways to try it.

So is it possible to write something in this format at all and how do you, in that case, do it?

Example code - what do I put at ???

var AWS = require('aws-sdk');

var dynamodb = new AWS.DynamoDB();

exports.handler = function(event, context) {

    var tableName = "GDCCompetition";

    var datetime = new Date().getTime().toString();

    DynamoDB.putItem({

        "TableName": tableName,

        "Item": {

            "datetime": {

                "N": datetime

            },

            "teamName": {

                "S": event.teamName

            },

            "members": ???

        }

    });

}

1 Answer

0 votes
by (44.4k points)

The documentation isn't very obvious, however, there's a factor known as DocClient, you can pass a usual JS object to it and it'll do all the parsing and transformation into AWS object (with all the types). You can use it like this:

var AWS = require("aws-sdk");

var DynamoDB = new AWS.DynamoDB.DocumentClient();

var params = {

    TableName: "MyTable",

    Item: {

        "teamName": "Team Awesome",

        "members": [

            {

                "email": "[email protected]",

                "name": "Bob"

            },

            {

                "email": "[email protected]",

                "name": "Alice"

            }

         ]

    } 

};

DynamoDB.put(params, function (err) {

     if (err) {

         return throw err;

     }

     //this is put

});

Read AWS Lambda to get more information about this computing service. In addition, you can also enroll in AWS Training to get better knowledge about AWS.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

Browse Categories

...