我有可下载的DynamoDB本地(目前正在运行)。我已经阅读了他们的文档,他们的示例代码正在工作。
我使用名称" Users“创建了Users表
这是UsersCreateTable.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
<script>
AWS.config.update({
region: "us-west-2",
endpoint: 'http://localhost:8000/',
// accessKeyId default can be used while using the downloadable version of DynamoDB.
// For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
accessKeyId: "fakeMyKeyId",
// secretAccessKey default can be used while using the downloadable version of DynamoDB.
// For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
secretAccessKey: "fakeSecretAccessKey"
});
var dynamodb = new AWS.DynamoDB();
function createUsers() {
var params = {
TableName : "Users",
KeySchema: [
{ AttributeName: "id", KeyType: "HASH"}
],
AttributeDefinitions: [
{ AttributeName: "id", AttributeType: "N" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
};
dynamodb.createTable(params, function(err, data) {
if (err) {
document.getElementById('textarea').innerHTML = "Unable to create users table: " + "\n" + JSON.stringify(err, undefined, 2);
} else {
document.getElementById('textarea').innerHTML = "Created users table: " + "\n" + JSON.stringify(data, undefined, 2);
}
});
}
</script>
<title></title>
</head>
<body>
<input id="createTableButton" type="button" value="Create Table" onclick="createUsers();" />
<br><br>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>
</body>
</html>我已经为我们的用户表准备了样本JSON数据。我修改了他们的MoviesLoadTable.html文件,并将其转换为UsersLoadTable.html,它将JSON数据上传到DynamoDB本地,以便加载我的JSON用户的数据。
当我试图加载我的JSON数据时,我在控制台上得到以下错误:
Uncaught SyntaxError: Unexpected token : in JSON at position 497
at JSON.parse (<anonymous>)
at FileReader.r.onload (UsersLoadData.html:31)
r.onload @ UsersLoadData.html:31
FileReader (async)
processFile @ UsersLoadData.html:53这是UsersLoadData.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
<script type="text/javascript">
AWS.config.update({
region: "us-west-2",
endpoint: 'http://localhost:8000/',
// accessKeyId default can be used while using the downloadable version of DynamoDB.
// For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
accessKeyId: "fakeMyKeyId",
// secretAccessKey default can be used while using the downloadable version of DynamoDB.
// For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
secretAccessKey: "fakeSecretAccessKey"
});
var docClient = new AWS.DynamoDB.DocumentClient();
function processFile(evt) {
document.getElementById('textarea').innerHTML = "";
document.getElementById('textarea').innerHTML += "Importing users into DynamoDB. Please wait..." + "\n";
var file = evt.target.files[0];
if (file) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
var allUsers = JSON.parse(contents);
allUsers.forEach(function (user) {
document.getElementById('textarea').innerHTML += "Processing user id: " + user.id + "\n";
var params = {
TableName: "Users",
Item: {
"id": user.id,
"info": user.info
}
};
docClient.put(params, function (err, data) {
if (err) {
document.getElementById('textarea').innerHTML += "Unable to add user: " + count + user.id + "\n";
document.getElementById('textarea').innerHTML += "Error JSON: " + JSON.stringify(err) + "\n";
} else {
document.getElementById('textarea').innerHTML += "Loading succeeded(id): " + user.id + "\n";
textarea.scrollTop = textarea.scrollHeight;
}
});
});
};
r.readAsText(file);
} else {
alert("Could not read users data file");
}
}
</script>
<title></title>
</head>
<body>
<input type="file" id="fileinput" accept='application/json' />
<br><br>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>
<script>
document.getElementById('fileinput').addEventListener('change', processFile, false);
</script>
</body>
</html>我搜索了错误,但找不到令人满意的解决方案。谢谢你的帮助。
https://stackoverflow.com/questions/49189207
复制相似问题