DynamoDB和Node Js:意外的令牌h

DynamoDB and Node Js : Unexpected token h

本文关键字:令牌 意外 Node Js DynamoDB      更新时间:2023-09-26

我正试图与DynamoDB竞争Node js amazon入门指南。我正在尝试创建一个表,但这是我遇到的错误:

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

我正在运行以下节点(直接从亚马逊入门指南中获取):

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));
    }
});

基于本教程,我在8080端口上运行了一个本地web服务器:https://www.youtube.com/watch?v=pU9Q6oiQNd0.它似乎运行得很好。

谢谢。

您正在将AWS端点设置为http://localhost:8000。这使得AWS SDK将AWS API调用发送到该URL,而不是亚马逊的服务器。你确定这就是你想要的吗?除非您在本地运行DynamoDB的一个版本,否则它将为每个DynamoDB请求向您自己的服务器发出请求,并尝试将结果解释为JSON。

SDK通常会根据区域正确设置端点,因此通常不需要手动设置。尝试在没有端点设置的情况下配置AWS:

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