从 Amazon DynamoDB 获取数据时遇到问题
Trouble getting data from Amazon DynamoDB
我在从 DynamoDB table 获取所有项目时遇到一些问题。我正在按照 AWS GitHub repo 中的示例进行操作。这是我的代码
import {APIGatewayProxyEventV2, APIGatewayProxyResultV2} from 'aws-lambda';
import {DynamoDB} from 'aws-sdk'
const dynamoClient = new DynamoDB.DocumentClient();
export async function main(
event: APIGatewayProxyEventV2,
): Promise<APIGatewayProxyResultV2> {
console.log('event ', JSON.stringify(event, null, 2));
const tableName: string = process.env.TABLE_NAME || ""
const requesterUserGroup = event.pathParameters?.userGroup || ""
console.log(`Table Name: ${tableName}`)
console.log(`User group: ${requesterUserGroup}`)
if(tableName.length == 0) return sendError("No table name provided", 500)
if(requesterUserGroup.length == 0) return sendError("Requester user group is empty", 400)
try {
const params = {
TableName: tableName
};
const response = await dynamoClient.scan(params)
// console.log(`response: ${response} `)
return sendSuccess(response.Items)
}catch (dbError){
console.log(dbError)
return sendError(dbError, 500)
}
}
function sendSuccess(message: any): APIGatewayProxyResultV2 {
return {
statusCode: 200,
body: JSON.stringify({ message })
}
}
function sendError(message: string, statusCode: number): APIGatewayProxyResultV2 {
return {
statusCode: statusCode,
body: JSON.stringify({ message })
}
}
这是我从中得到的回复
{
"statusCode": 200,
"body": "{}"
}
我已经仔细检查了 table 名称并且我在 table 中有项目(1000 项!)。
问题:为什么我没有从 table 获得任何物品?
您错过了 .promise()
,因此在请求完成之前不会等待。
const response = await dynamoClient.scan(params).promise();
我在从 DynamoDB table 获取所有项目时遇到一些问题。我正在按照 AWS GitHub repo 中的示例进行操作。这是我的代码
import {APIGatewayProxyEventV2, APIGatewayProxyResultV2} from 'aws-lambda';
import {DynamoDB} from 'aws-sdk'
const dynamoClient = new DynamoDB.DocumentClient();
export async function main(
event: APIGatewayProxyEventV2,
): Promise<APIGatewayProxyResultV2> {
console.log('event ', JSON.stringify(event, null, 2));
const tableName: string = process.env.TABLE_NAME || ""
const requesterUserGroup = event.pathParameters?.userGroup || ""
console.log(`Table Name: ${tableName}`)
console.log(`User group: ${requesterUserGroup}`)
if(tableName.length == 0) return sendError("No table name provided", 500)
if(requesterUserGroup.length == 0) return sendError("Requester user group is empty", 400)
try {
const params = {
TableName: tableName
};
const response = await dynamoClient.scan(params)
// console.log(`response: ${response} `)
return sendSuccess(response.Items)
}catch (dbError){
console.log(dbError)
return sendError(dbError, 500)
}
}
function sendSuccess(message: any): APIGatewayProxyResultV2 {
return {
statusCode: 200,
body: JSON.stringify({ message })
}
}
function sendError(message: string, statusCode: number): APIGatewayProxyResultV2 {
return {
statusCode: statusCode,
body: JSON.stringify({ message })
}
}
这是我从中得到的回复
{
"statusCode": 200,
"body": "{}"
}
我已经仔细检查了 table 名称并且我在 table 中有项目(1000 项!)。
问题:为什么我没有从 table 获得任何物品?
您错过了 .promise()
,因此在请求完成之前不会等待。
const response = await dynamoClient.scan(params).promise();