Jim
Jim

Reputation: 1608

How to use AWS SDK with Promise

At first, I use Simplified Callback Method work fine. Now, I want to use promise to query AWS dynamoDB. I refer this.

But always 500 Internal Server Error. I use lambda and node.js 4.3. Do I miss something?

Handler.js

let AWS = require('aws-sdk');
AWS.config.setPromisesDependency(null);
docClient = new AWS.DynamoDB.DocumentClient();

module.exports.handler = (event, context, callback) => {

  const listObjectPromise = docClient.query(params).promise();
  listObjectPromise.then((data) => {
      return callback(null, data);
  }).catch((err) => {
      return callback(err, null);
  });
};

Upvotes: 1

Views: 2363

Answers (1)

matsev
matsev

Reputation: 33779

Copied from your reference:

By default, the AWS SDK for JavaScript will check for a globally defined Promise function. If found, it adds the promise() method on AWS.Request objects. Some environments, such as Internet Explorer or earlier versions of Node.js, don't support promises natively. You can use the AWS.config.setPromisesDependency() method to supply a Promise constructor.

Since you are using a Node.js 4.3 environment you already have promise support, i.e. you do not need to call the setPromiseDependecy() function. My suspicion is that since you are calling the function with a null argument, the AWS SDK throws a NPE when it attempts to create a new promise, which results in the 500 error. My advice is to simply delete this function call.

Upvotes: 1

Related Questions