0

I'm new to working with databases through scripting, so I was learning with this tutorial on how to access DynamoDB with HTML and JavaScript. I successfully created the Movies table and added the one item, but I'm running into an error with reading the item. Below is the example script for reading an item with my credentials.

<html>
<head>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js">
</script>

<script>
AWS.config.update({
  region: "us-east-2",
  endpoint: 'http://localhost:8000',
  accessKeyId: "...",
  secretAccessKey: "..."
});

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

function readItem() {
     var table = "Movies";
     var year = 2015;
     var title = "The Big New Movie";

    var params = {
        TableName: table,
        Key:{
            "year": year,
            "title": title
        }
     };
    docClient.get(params, function(err, data) {
         if (err) {
             document.getElementById('textarea').innerHTML = "Unable to read item: " + "\n" + JSON.stringify(err, undefined, 2);
         } else {
            document.getElementById('textarea').innerHTML = "GetItem succeeded: " + "\n" + JSON.stringify(data, undefined, 2);
        }
    });
}

</script>
</head>

<body>
<input id="readItem" type="button" value="Read Item" onclick="readItem();" />
<br><br>
<textarea readonly id= "textarea" style="width:400px; height:800px">
</textarea>

</body>
</html>

After clicking the button in the HTML file, I get this error:

Unable to read item: 
{
  "message": "Network Failure",
  "code": "NetworkingError",
  "time": "2017-08-02T14:32:36.416Z",
  "region": "us-east-2",
  "hostname": "localhost",
  "retryable": true
}

I have also tried query and scan with the demo code and I get the same result. The only idea I have is the local host port might not be correct, but I'm not sure how to check what port I can use instead. I've also read a few forum threads about similar issues, but they used the database with another Amazon web service. Any other help would be appreciated!

1 Answer 1

1

Please ensure that the DynamoDB is running on your local machine. Please refer this page.

1) Run the DynamoDB local

2) Make sure you have the table called Movies with the key values present in html. Then, you should get the data in your text area

I have tested the code. It works fine for me.

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

3 Comments

Is there any way to make this work without a local version of DynamoDB?
You either need to connect to Dynamodb local or dynamodb running on AWS. Otherwise, where will you get the data from?
Oh, because the endpoint has to be local for getting requests you would need the local version of the database, I somehow didn't realize that after all that time, thank you!

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.