从 dynamoDB getItem 导出值
export values from dynamoDB getItem
我正在尝试从 dynamoDB 获取 getItem 函数之外的数据,但出现此错误,我不知道为什么。
Error ValidationException: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes
这是我的代码
const aws = require("aws-sdk"),
docClient = new aws.DynamoDB.DocumentClient({ apiVersion: "2012-08-10" }),
ddb = new aws.DynamoDB({ apiVersion: "2012-08-10" }),
tableName = "usersDB",
exports.apiKey = async (req, res, context) => {
var params = {
TableName: tableName,
Key: {
"username": { "S": req.body.username },
},
ProjectionExpression: "apiKey"
};
chaveAPI = await ddb.getItem(params, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Item);
}
});
};
EDIT1:修复了验证错误但仍然无法从 dyname 获取数据。这里是固定的
exports.apiKey = async (req, res) => {
console.log("comecei")
var params = {
TableName: tableName,
Key: {
'username': {S: req.user.username}
},
ProjectionExpression: 'apiKey'
};
// Call DynamoDB to read the item from the table
dataFromDynamo = await ddb.getItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Item);
return data.Item
}
});
console.log(dataFromDynamo)
};
我从您的代码示例中注意到一些事情:
- 您将回调与 async/await 混合使用。
- 您没有声明
dataFromDynamo
变量。
而不是这个
// Call DynamoDB to read the item from the table
dataFromDynamo = await ddb.getItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Item);
return data.Item
}
});
这样做
try {
// Call DynamoDB to read the item from the table
const dataFromDynamo = await ddb.getItem(params).promise()
console.log("Success", data.Item);
return data.Item
} catch (err) {
console.log("Error", err);
}
我正在尝试从 dynamoDB 获取 getItem 函数之外的数据,但出现此错误,我不知道为什么。
Error ValidationException: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes
这是我的代码
const aws = require("aws-sdk"),
docClient = new aws.DynamoDB.DocumentClient({ apiVersion: "2012-08-10" }),
ddb = new aws.DynamoDB({ apiVersion: "2012-08-10" }),
tableName = "usersDB",
exports.apiKey = async (req, res, context) => {
var params = {
TableName: tableName,
Key: {
"username": { "S": req.body.username },
},
ProjectionExpression: "apiKey"
};
chaveAPI = await ddb.getItem(params, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Item);
}
});
};
EDIT1:修复了验证错误但仍然无法从 dyname 获取数据。这里是固定的
exports.apiKey = async (req, res) => {
console.log("comecei")
var params = {
TableName: tableName,
Key: {
'username': {S: req.user.username}
},
ProjectionExpression: 'apiKey'
};
// Call DynamoDB to read the item from the table
dataFromDynamo = await ddb.getItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Item);
return data.Item
}
});
console.log(dataFromDynamo)
};
我从您的代码示例中注意到一些事情:
- 您将回调与 async/await 混合使用。
- 您没有声明
dataFromDynamo
变量。
而不是这个
// Call DynamoDB to read the item from the table
dataFromDynamo = await ddb.getItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Item);
return data.Item
}
});
这样做
try {
// Call DynamoDB to read the item from the table
const dataFromDynamo = await ddb.getItem(params).promise()
console.log("Success", data.Item);
return data.Item
} catch (err) {
console.log("Error", err);
}