forked from amazon-archives/aws-full-stack-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListGoals.js
45 lines (40 loc) · 1.35 KB
/
ListGoals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
const params = {
TableName: process.env.TABLE_NAME, //[ProjectName]-Goals
// 'KeyConditionExpression' defines the condition for the query
// - 'userId = :userId': only return items with matching 'userId' partition key
// 'ExpressionAttributeValues' defines the value in the condition
// - ':userId': defines 'userId' to be Identity Pool identity id of the
// authenticated user
KeyConditionExpression: "userId = :userId",
ExpressionAttributeValues: {
":userId": event.requestContext.identity.cognitoIdentityId
}
};
dynamoDb.query(params, (error, data) => {
// Set response headers to enable CORS (Cross-Origin Resource Sharing)
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials" : true
};
// Return status code 500 on error
if (error) {
const response = {
statusCode: 500,
headers: headers,
body: error
};
callback(null, response);
return;
}
// Return status code 200 and the retrieved items on success
const response = {
statusCode: 200,
headers: headers,
body: JSON.stringify(data.Items)
};
callback(null, response);
});
}