我使用DynamoDB存储数据并尝试使用JavaScript读取项目。我有一个函数将从指定的表中读取一个项目,但我想检测表中的项目不存在:
[
{ "firstName":"Tom", "lastName":"Jackson"},
{ "firstName":"Jenny", "lastName":"Mary"},
{ "firstName":"Red", "lastName":"Blue"},
{ "firstName":"Jason", "lastName":"John"},
{ "firstName":"May", "lastName":"Black"}
]
问题是,即使表格中没有项目,function getChapterLocations() {
var table = bibleSelect.value.replace(/\s/g, '');
var chapter = bookSelect.value + " " + chapterSelect.value;
var params = {
TableName: table,
Key:{
"chapter": chapter
}
};
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.get(params, function(err, data) {
if (err) {
currentLocations = {};
} else {
currentLocations = data.Item.locations;
}
getText();
});
}
始终为err
。当一个项目不存在时,我得到的是一个在控制台中看起来像这样的错误:
callListeners https://sdk.amazonaws.com/js/aws-sdk-2.164.0.min.js:48:1027 发出https://sdk.amazonaws.com/js/aws-sdk-2.164.0.min.js:48:695 emitEvent https://sdk.amazonaws.com/js/aws-sdk-2.164.0.min.js:47:18756
我会忽略此错误,但是当某个项目不存在时,它会阻止调用null
。