Simon Breton
Simon Breton

Reputation: 2876

DynamoDB and Node Js : Unexpected token h

I'm trying to compete the Node js amazon start guide with DynamoDB. I'm trying to create a table but here is the error I've :

Unable to create table. Error JSON: {
  "message": "Unexpected token h",
  "code": "SyntaxError",
  "time": "2016-05-06T16:59:50.411Z",
  "statusCode": 200,
  "retryable": false,
  "retryDelay": 0

I'm running the following node (taken directly from amazon start guide) :

var AWS = require("aws-sdk");
AWS.config.loadFromPath('./.aws/credentials.json'); 

AWS.config.update({
  region: "us-west-2",
  endpoint: "http://localhost:8000"
});

var dynamodb = new AWS.DynamoDB();

var params = {
    TableName : "Movies",
    KeySchema: [       
        { AttributeName: "year", KeyType: "HASH"},  //Partition key
        { AttributeName: "title", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [       
        { AttributeName: "year", AttributeType: "N" },
        { AttributeName: "title", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

I've run a local web server on port 8080 based on this tutorial : https://www.youtube.com/watch?v=pU9Q6oiQNd0. It seems to be working fine.

Thanks.

Upvotes: 1

Views: 2391

Answers (1)

ataylor
ataylor

Reputation: 66109

You are setting the AWS endpoint to http://localhost:8000. This makes the AWS SDK send AWS API calls to that URL instead of Amazon's servers. Are you sure that's what you want? Unless you're running a version of DynamoDB locally, that will make a request to your own server for each DynamoDB request and try to interpret the result as JSON.

The SDK will usually set the endpoint correctly based on region, so there's generally no need to set it manually. Try configuring AWS without the endpoint setting:

AWS.config.update({
    region: "us-west-2"
});

Upvotes: 4

Related Questions