我在從 DynamoDB 表中獲取所有專案時遇到了一些問題。我正在關注AWS GitHub 存盤庫中的示例。這是我的代碼
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": "{}"
}
我已經仔細檢查了表名,并且表中有專案(1000 個專案!)。
問題:為什么我沒有從桌子上得到任何物品?
uj5u.com熱心網友回復:
你錯過了.promise(),所以它不會等待請求完成。
const response = await dynamoClient.scan(params).promise();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/450566.html
