Search code examples
javascriptamazon-web-servicesaws-sdkaws-sdk-js

Can't get a put to work with AWS SDK for Javascript


The AWS tutorial with my Cognito information works like a charm. However, when I ported it over to a different HTML file for some reason the put doesn't work. I can use the same list function to query my account which suggests my credentials work (dynamodb and docClient have same permissions?). I've been banging my head against the screen and just can't seem to put a line in the database. Is this a NoSQL thing that I'm missing b/c of key values?

    var dynamodb = new AWS.DynamoDB();
    var docClient = new AWS.DynamoDB.DocumentClient();
    var my_form = {};

    function getFormData(log_date,notes){ 
        my_form.log_date=document.getElementById('log_date').value;
        my_form.notes=document.getElementById('parent_notes').value;
        console.log("I got form data of "+my_form.log_date+" and "+my_form.notes);
        /* some other fields */
        /* now call ur function by passing the above values */
    }

This part doesn't work :(

    function createItem() {
        getFormData();
        console.log("in createItem:"+my_form.log_date+" and "+my_form.notes);
        var params = {
            TableName :"Infant_Logs",
            Item:{
                "year": 2011,
                "title": "testing",
                "Date": "something",
                "Name": "Daniel",
                "Notes": "none"
                /*"info":{
                    "plot": "Nothing happens at all.",
                    "rating": 0
                }*/
            }
        };

        console.log("I am params: "+JSON.stringify(params));
        docClient.put(params, function(err, data) {
            if (err) {
                //console.log("Unable to add item: "+"\n"+JSON.stringify(err, undefined,2));
                document.getElementById('textarea').innerHTML = "Unable to add item: " + "\n" + JSON.stringify(err, undefined, 2);
            } else {
                //console.log("PutItem succeeded: "+"\n"+JSON.stringify(data,undefined,2));
                document.getElementById('textarea').innerHTML = "PutItem succeeded: " + "\n" + JSON.stringify(data, undefined, 2);
            }
        });
        console.log("The end of creation");
    }

This part works

    function listMovies() {
        var params = {};
        dynamodb.listTables(params, function(err, data) {
            if (err){
                console.log("I totally err");
                document.getElementById('textarea').innerHTML = "Unable to list tables: " + "\n" + JSON.stringify(err, undefined, 2);
            }
            else{
                console.log("I totally werk "+JSON.stringify(data,undefined,2));
                document.getElementById('textarea').innerHTML = "List of tables: " + "\n" + JSON.stringify(data, undefined, 2);
            }
        });
    }

Solution

  • I think I know what happened. The NoSQL format wants a string. The javascript variables were objects (or something) so once I added .toString() it started popping up. I'm still not seeing the success log but at least it is populating in the DB. shrug