DynamoDB.putItem function not saving item with sls command











up vote
0
down vote

favorite












I am having trouble with my Lambda function with Serverless framework, which should be putting data to DynamoDB. I'm unsure of the issue as I am getting no error message in the console when I run: sls invoke local -f scrape -d 'the-last-bookstore-los-angeles.



saveRatingsToDB.js



const uuid = require('uuid');
const AWS = require('aws-sdk');

const dynamoDb = new AWS.DynamoDB.DocumentClient();

module.exports = (data, businessName) => {
console.log('data saving...');
console.log(data);
const params = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
id: uuid.v1(),
businessName: businessName,
reviewCount: data.reviewCount,
rating: data.rating,
createdAt: JSON.stringify(new Date())
}
};

// I am can log params okay, but nothing with dynamoDb.put is logged to the console

dynamoDb.put(params, error => {
console.log('putting data');
if (error) {
console.log(`Error saving data to DynamoDB: ${JSON.stringify(error)}`);
return Promise.reject(
`Error saving data to DynamoDB: ${JSON.stringify(error)}`
);
} else {
console.log('data saved');
return Promise.resolve(params.Item);
}
});
};


handler.js



'use strict';
const { getPage, parsePage, saveRatingsToDB } = require('./utils');

module.exports.scrape = async (event, context) => {
console.log('Function triggered', event);
const name = event;
const page = await getPage(name);
const data = await parsePage(page);
const db = await saveRatingsToDB(data, name);

return db;
};


serverless.yaml



provider:
name: aws
runtime: nodejs8.10
stage: dev
region: eu-west-2
environment:
DYNAMODB_TABLE: my-table-name
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn_reference_"
package:
include:
- utils/**


As mentioned, I get no error message in the console. I can the log and see the params object from the saveRatingsToDB.js, but nothing within the actual dynamoDb.put function.



Any help would be appreciated.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I am having trouble with my Lambda function with Serverless framework, which should be putting data to DynamoDB. I'm unsure of the issue as I am getting no error message in the console when I run: sls invoke local -f scrape -d 'the-last-bookstore-los-angeles.



    saveRatingsToDB.js



    const uuid = require('uuid');
    const AWS = require('aws-sdk');

    const dynamoDb = new AWS.DynamoDB.DocumentClient();

    module.exports = (data, businessName) => {
    console.log('data saving...');
    console.log(data);
    const params = {
    TableName: process.env.DYNAMODB_TABLE,
    Item: {
    id: uuid.v1(),
    businessName: businessName,
    reviewCount: data.reviewCount,
    rating: data.rating,
    createdAt: JSON.stringify(new Date())
    }
    };

    // I am can log params okay, but nothing with dynamoDb.put is logged to the console

    dynamoDb.put(params, error => {
    console.log('putting data');
    if (error) {
    console.log(`Error saving data to DynamoDB: ${JSON.stringify(error)}`);
    return Promise.reject(
    `Error saving data to DynamoDB: ${JSON.stringify(error)}`
    );
    } else {
    console.log('data saved');
    return Promise.resolve(params.Item);
    }
    });
    };


    handler.js



    'use strict';
    const { getPage, parsePage, saveRatingsToDB } = require('./utils');

    module.exports.scrape = async (event, context) => {
    console.log('Function triggered', event);
    const name = event;
    const page = await getPage(name);
    const data = await parsePage(page);
    const db = await saveRatingsToDB(data, name);

    return db;
    };


    serverless.yaml



    provider:
    name: aws
    runtime: nodejs8.10
    stage: dev
    region: eu-west-2
    environment:
    DYNAMODB_TABLE: my-table-name
    iamRoleStatements:
    - Effect: Allow
    Action:
    - dynamodb:Query
    - dynamodb:Scan
    - dynamodb:GetItem
    - dynamodb:PutItem
    - dynamodb:UpdateItem
    - dynamodb:DeleteItem
    Resource: "arn_reference_"
    package:
    include:
    - utils/**


    As mentioned, I get no error message in the console. I can the log and see the params object from the saveRatingsToDB.js, but nothing within the actual dynamoDb.put function.



    Any help would be appreciated.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am having trouble with my Lambda function with Serverless framework, which should be putting data to DynamoDB. I'm unsure of the issue as I am getting no error message in the console when I run: sls invoke local -f scrape -d 'the-last-bookstore-los-angeles.



      saveRatingsToDB.js



      const uuid = require('uuid');
      const AWS = require('aws-sdk');

      const dynamoDb = new AWS.DynamoDB.DocumentClient();

      module.exports = (data, businessName) => {
      console.log('data saving...');
      console.log(data);
      const params = {
      TableName: process.env.DYNAMODB_TABLE,
      Item: {
      id: uuid.v1(),
      businessName: businessName,
      reviewCount: data.reviewCount,
      rating: data.rating,
      createdAt: JSON.stringify(new Date())
      }
      };

      // I am can log params okay, but nothing with dynamoDb.put is logged to the console

      dynamoDb.put(params, error => {
      console.log('putting data');
      if (error) {
      console.log(`Error saving data to DynamoDB: ${JSON.stringify(error)}`);
      return Promise.reject(
      `Error saving data to DynamoDB: ${JSON.stringify(error)}`
      );
      } else {
      console.log('data saved');
      return Promise.resolve(params.Item);
      }
      });
      };


      handler.js



      'use strict';
      const { getPage, parsePage, saveRatingsToDB } = require('./utils');

      module.exports.scrape = async (event, context) => {
      console.log('Function triggered', event);
      const name = event;
      const page = await getPage(name);
      const data = await parsePage(page);
      const db = await saveRatingsToDB(data, name);

      return db;
      };


      serverless.yaml



      provider:
      name: aws
      runtime: nodejs8.10
      stage: dev
      region: eu-west-2
      environment:
      DYNAMODB_TABLE: my-table-name
      iamRoleStatements:
      - Effect: Allow
      Action:
      - dynamodb:Query
      - dynamodb:Scan
      - dynamodb:GetItem
      - dynamodb:PutItem
      - dynamodb:UpdateItem
      - dynamodb:DeleteItem
      Resource: "arn_reference_"
      package:
      include:
      - utils/**


      As mentioned, I get no error message in the console. I can the log and see the params object from the saveRatingsToDB.js, but nothing within the actual dynamoDb.put function.



      Any help would be appreciated.










      share|improve this question















      I am having trouble with my Lambda function with Serverless framework, which should be putting data to DynamoDB. I'm unsure of the issue as I am getting no error message in the console when I run: sls invoke local -f scrape -d 'the-last-bookstore-los-angeles.



      saveRatingsToDB.js



      const uuid = require('uuid');
      const AWS = require('aws-sdk');

      const dynamoDb = new AWS.DynamoDB.DocumentClient();

      module.exports = (data, businessName) => {
      console.log('data saving...');
      console.log(data);
      const params = {
      TableName: process.env.DYNAMODB_TABLE,
      Item: {
      id: uuid.v1(),
      businessName: businessName,
      reviewCount: data.reviewCount,
      rating: data.rating,
      createdAt: JSON.stringify(new Date())
      }
      };

      // I am can log params okay, but nothing with dynamoDb.put is logged to the console

      dynamoDb.put(params, error => {
      console.log('putting data');
      if (error) {
      console.log(`Error saving data to DynamoDB: ${JSON.stringify(error)}`);
      return Promise.reject(
      `Error saving data to DynamoDB: ${JSON.stringify(error)}`
      );
      } else {
      console.log('data saved');
      return Promise.resolve(params.Item);
      }
      });
      };


      handler.js



      'use strict';
      const { getPage, parsePage, saveRatingsToDB } = require('./utils');

      module.exports.scrape = async (event, context) => {
      console.log('Function triggered', event);
      const name = event;
      const page = await getPage(name);
      const data = await parsePage(page);
      const db = await saveRatingsToDB(data, name);

      return db;
      };


      serverless.yaml



      provider:
      name: aws
      runtime: nodejs8.10
      stage: dev
      region: eu-west-2
      environment:
      DYNAMODB_TABLE: my-table-name
      iamRoleStatements:
      - Effect: Allow
      Action:
      - dynamodb:Query
      - dynamodb:Scan
      - dynamodb:GetItem
      - dynamodb:PutItem
      - dynamodb:UpdateItem
      - dynamodb:DeleteItem
      Resource: "arn_reference_"
      package:
      include:
      - utils/**


      As mentioned, I get no error message in the console. I can the log and see the params object from the saveRatingsToDB.js, but nothing within the actual dynamoDb.put function.



      Any help would be appreciated.







      node.js aws-lambda amazon-dynamodb serverless-framework






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 0:42









      John Rotenstein

      64.9k770113




      64.9k770113










      asked Nov 18 at 13:14









      Tim Rooke

      14010




      14010
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You tried to run saveRatingsToDB as an asynchronous function by doing:



          const db = await saveRatingsToDB(data, name);


          Unfortunately, with the way this function is written currently, it's not an asynchronous function. The reason while you don't see the log inside dynamoDb.put is because the saveRatingsToDB function doesn't wait for the asynchronous callback to finish but return earlier.



          To make saveRatingsToDB properly an asynchronous function and force it to wait for the callback, you can return a dynamoDb.put as a promise from that function:



          module.exports = (data, businessName) => {
          ...

          // use dynamoDb.put().promise() to return a promise from dynamoDb transaction
          return dynamoDb.put(params, error => {
          ...
          // Do whatever with the error
          }).promise();
          };





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361254%2fdynamodb-putitem-function-not-saving-item-with-sls-command%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            You tried to run saveRatingsToDB as an asynchronous function by doing:



            const db = await saveRatingsToDB(data, name);


            Unfortunately, with the way this function is written currently, it's not an asynchronous function. The reason while you don't see the log inside dynamoDb.put is because the saveRatingsToDB function doesn't wait for the asynchronous callback to finish but return earlier.



            To make saveRatingsToDB properly an asynchronous function and force it to wait for the callback, you can return a dynamoDb.put as a promise from that function:



            module.exports = (data, businessName) => {
            ...

            // use dynamoDb.put().promise() to return a promise from dynamoDb transaction
            return dynamoDb.put(params, error => {
            ...
            // Do whatever with the error
            }).promise();
            };





            share|improve this answer

























              up vote
              1
              down vote



              accepted










              You tried to run saveRatingsToDB as an asynchronous function by doing:



              const db = await saveRatingsToDB(data, name);


              Unfortunately, with the way this function is written currently, it's not an asynchronous function. The reason while you don't see the log inside dynamoDb.put is because the saveRatingsToDB function doesn't wait for the asynchronous callback to finish but return earlier.



              To make saveRatingsToDB properly an asynchronous function and force it to wait for the callback, you can return a dynamoDb.put as a promise from that function:



              module.exports = (data, businessName) => {
              ...

              // use dynamoDb.put().promise() to return a promise from dynamoDb transaction
              return dynamoDb.put(params, error => {
              ...
              // Do whatever with the error
              }).promise();
              };





              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                You tried to run saveRatingsToDB as an asynchronous function by doing:



                const db = await saveRatingsToDB(data, name);


                Unfortunately, with the way this function is written currently, it's not an asynchronous function. The reason while you don't see the log inside dynamoDb.put is because the saveRatingsToDB function doesn't wait for the asynchronous callback to finish but return earlier.



                To make saveRatingsToDB properly an asynchronous function and force it to wait for the callback, you can return a dynamoDb.put as a promise from that function:



                module.exports = (data, businessName) => {
                ...

                // use dynamoDb.put().promise() to return a promise from dynamoDb transaction
                return dynamoDb.put(params, error => {
                ...
                // Do whatever with the error
                }).promise();
                };





                share|improve this answer












                You tried to run saveRatingsToDB as an asynchronous function by doing:



                const db = await saveRatingsToDB(data, name);


                Unfortunately, with the way this function is written currently, it's not an asynchronous function. The reason while you don't see the log inside dynamoDb.put is because the saveRatingsToDB function doesn't wait for the asynchronous callback to finish but return earlier.



                To make saveRatingsToDB properly an asynchronous function and force it to wait for the callback, you can return a dynamoDb.put as a promise from that function:



                module.exports = (data, businessName) => {
                ...

                // use dynamoDb.put().promise() to return a promise from dynamoDb transaction
                return dynamoDb.put(params, error => {
                ...
                // Do whatever with the error
                }).promise();
                };






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 at 3:49









                Thai Duong Tran

                1,28858




                1,28858






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361254%2fdynamodb-putitem-function-not-saving-item-with-sls-command%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin