来自外部函数的 Dynamo DB 查询的 AWS Lambda NodeJS 访问结果



我正在尝试查询 Dynomo DB 表,我想在我的 AWS Lambda 中的函数中浏览结果项目。我无法从 Dynamo DB 查询中提取结果。它在闭包内部,我可以控制台记录它,但我无法为外部函数范围内的任何变量分配它。 我应该怎么做才能把它弄到外面?

function check(id) {
//build params
let params = {
TableName: 'demo_table',
KeyConditionExpression: #key =: id,
Limit: 5,
ScanIndexForward: false,
ExpressionAttributeNames: {
#key: process.env.PRIMARYKEY
},
ExpressionAttributeValues: {
: id: id
}
};
//query ddb
let result = {};
ddb.query(params, function(err, data) {
if (err) {
console.log("AN ERROR OCCUREDn");
console.log(err);
} else {
//How to copy the data from here to outside??
//I can console log and see the data
result = data;
}
});
console.log(result); //returns {}
}

const check = async (id) => {
//build params
let params = {
TableName: 'demo_table',
KeyConditionExpression: #key =: id,
Limit: 5,
ScanIndexForward: false,
ExpressionAttributeNames: {
#
key: process.env.PRIMARYKEY
},
ExpressionAttributeValues: {
: id: id
}
};

let result = await new Promise((resolve, rejects) => {
ddb.query(params, function (err, data) {
if (err) rejects(err)
resolve(data)
});
})

console.log(result); //returns {}
}

通过使用承诺,您可以获取数据。 数据库读取是一种异步操作。

最新更新