@@ -8,65 +8,65 @@ chapter: All listings query
88postnumber : 12
99---
1010
11- In this part we get the data from Dynamo And return it.
11+ In this part we get the data from DynamoDB And return it.
1212
13- Back in our query.js file we can import the dynamoDB wrapper at the top of the file:
13+ Back in our ` query.js ` file we need to import the DynamoDB wrapper at the top of the file:
1414
15- ```
16- import * as dynamoDBLib from "../../libs/dynamodb-lib";
15+ ``` javascript
16+ import * as dynamoDBLib from " ../../libs/dynamodb-lib"
1717```
1818
1919Next up we can create the parameters for our DynamoDB operation:
2020
21- ```
21+ ``` javascript
2222const params = {
23- TableName: process.env.ListingsDB || "dev-listings"
24- };
23+ TableName: process .env .ListingsDB || " dev-listings" ,
24+ }
2525```
2626
27- Just a simple object that has the tablename. reason why we have the or operator is for when we test, Jest cannot pickup the .env file.
27+ Just a simple object that has the tablename. reason why we have the or operator is for when we test, Jest cannot pickup the ` .env ` file.
2828
2929Next we have a try catch block that will help us fetch the data and return it:
3030
31- ```
32- try {
33- const result = await dynamoDBLib.call("scan", params);
34-
35- if (result.Items.length === 0) {
36- return "You have no listings";
37- } else {
38- return result.Items.map((i) => ({
39- listingId: i.listingId,
40- coverPhoto: i.coverPhoto,
41- listingName: i.listingName,
42- listingDescription: i.listingDescription,
43- listingType: i.listingType.map((m) => ({
44- name: m,
45- })),
46- listingLocation: i.listingLocation,
47- listingActivities: i.listingActivities.map((k) => ({
48- name: k,
49- })),
50- specialType: i.specialType,
51- specialAmount: i.specialAmount,
52- rating: i.rating,
53- guide: {
54- Name: i.guide.name,
55- Bio: i.guide.bio,
56- Avatar: i.guide.avatar,
57- },
58- price: i.price,
59- numberOfDays: i.numberOfDays,
60- }));
61- }
62-
63- // return result;
64- } catch (e) {
65- return {
66- message: e.message,
67- code: "500x",
68- };
31+ ``` javascript
32+ try {
33+ const result = await dynamoDBLib .call (" scan" , params)
34+
35+ if (result .Items .length === 0 ) {
36+ return " You have no listings"
37+ } else {
38+ return result .Items .map (i => ({
39+ listingId: i .listingId ,
40+ coverPhoto: i .coverPhoto ,
41+ listingName: i .listingName ,
42+ listingDescription: i .listingDescription ,
43+ listingType: i .listingType .map (m => ({
44+ name: m,
45+ })),
46+ listingLocation: i .listingLocation ,
47+ listingActivities: i .listingActivities .map (k => ({
48+ name: k,
49+ })),
50+ specialType: i .specialType ,
51+ specialAmount: i .specialAmount ,
52+ rating: i .rating ,
53+ guide: {
54+ Name: i .guide .name ,
55+ Bio: i .guide .bio ,
56+ Avatar: i .guide .avatar ,
57+ },
58+ price: i .price ,
59+ numberOfDays: i .numberOfDays ,
60+ }))
61+ }
62+
63+ // return result;
64+ } catch (e) {
65+ return {
66+ message: e .message ,
67+ code: " 500x" ,
6968 }
69+ }
7070```
7171
7272🔋First we return a promise and execute a scan on Dynamo and pass through our params object.
0 commit comments