Sha*_*eel 1 amazon-dynamodb aws-lambda serverless-framework
我正在使用无服务器框架来设置下表:
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: ap-southeast-1
environment:
DYNAMODB_TABLE: "Itinerary-${self:provider.stage}"
DYNAMODB_COUNTRY_INDEX: country_index
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}/index/${self:provider.environment.DYNAMODB_COUNTRY_INDEX}"
resources:
Resources:
ItineraryTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
-
AttributeName: ID
AttributeType: S
-
AttributeName: Country
AttributeType: S
KeySchema:
-
AttributeName: ID
KeyType: HASH
GlobalSecondaryIndexes:
-
IndexName: ${self:provider.environment.DYNAMODB_COUNTRY_INDEX}
KeySchema:
-
AttributeName: Country
KeyType: HASH
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:provider.environment.DYNAMODB_TABLE}
Run Code Online (Sandbox Code Playgroud)
现在,我能够执行“获取”操作,以按ID从数据库中检索项目。但是,如果我尝试在GSI上执行“查询”,则不会返回任何内容。没有失败,但是我从不找回数据。
以下是我的查询内容:
const dynamoDb = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: process.env.DYNAMODB_TABLE, // maps back to the serverless config variable above
IndexName: process.env.DYNAMODB_COUNTRY_INDEX, // maps back to the serverless config variable above
KeyConditionExpression: "Country=:con",
ExpressionAttributeValues: { ":con" : "Portugal" }
};
dynamoDb.query(params, (error, result) => {
// handle potential errors
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the Itineraries'+error,
});
return;
}
}
var json_result = JSON.stringify(result.Item);
Run Code Online (Sandbox Code Playgroud)
我应该在这里补充说,如果我也执行无过滤器“扫描”,则无法获取数据。如果尝试在AWS Dynamo Web门户上按索引(或其他方式)搜索项目,则可以得到结果。
我无法弄清楚这是怎么回事。任何人可以散发出的光将不胜感激。
好的,所以我找出了问题所在。我发布的问题中缺少一个关键声明(因为我认为这无关紧要),但事实证明确实如此。
我只是使用以下命令对查询结果进行字符串化:
var json_result = JSON.stringify(result.Item);
Run Code Online (Sandbox Code Playgroud)
对于上述工程“得到”,但对于一个查询,它需要result.Item 小号:
var json_result = JSON.stringify(result.Items);
Run Code Online (Sandbox Code Playgroud)
我很傻。感谢您的帮助!
附言:我已将声明添加到原始问题中,以便清楚
| 归档时间: |
|
| 查看次数: |
1509 次 |
| 最近记录: |