【发布时间】:2020-02-22 10:53:52
【问题描述】:
我将我的 dynamoDB 函数包装到另一个函数中,但在将其返回给“item”常量时遇到了问题。我在这里缺少什么?
作品:
const params = {
TableName: table,
Key: {
id: id
},
};
dynamoDb.get(params, (error, result) => {
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
body: 'Couldn\'t fetch the item.',
});
return;
}
const item = result.Item
})
不起作用(返回未定义):
const getFromDb = () => {
const params = {
TableName: table,
Key: {
id: id
},
};
dynamoDb.get(params, (error, result) => {
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
body: 'Couldn\'t fetch the item.',
});
return;
}
return result.Item
})
}
// Get from db
const item = getFromDb()
// do stuff with result item...
【问题讨论】:
标签: node.js asynchronous aws-lambda amazon-dynamodb