如何将 AWS SDK 与 Promise 结合使用
How to use AWS SDK with Promise
起初,我使用简化回调方法工作正常。现在,我想使用 promise 来查询 AWS dynamoDB。
我指的是 this.
但总是500 Internal Server Error。
我使用 lambda 和 node.js 4.3。我错过了什么吗?
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);
});
};
复制自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.
由于您使用的是 Node.js 4.3 环境,因此您已经有了 promise 支持,即您不需要调用 setPromiseDependecy() 函数。我怀疑是因为您使用 null
参数调用该函数,AWS SDK 在尝试创建新承诺时会抛出 NPE,从而导致 500 错误。我的建议是简单地删除这个函数调用。
起初,我使用简化回调方法工作正常。现在,我想使用 promise 来查询 AWS dynamoDB。 我指的是 this.
但总是500 Internal Server Error。 我使用 lambda 和 node.js 4.3。我错过了什么吗?
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);
});
};
复制自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.
由于您使用的是 Node.js 4.3 环境,因此您已经有了 promise 支持,即您不需要调用 setPromiseDependecy() 函数。我怀疑是因为您使用 null
参数调用该函数,AWS SDK 在尝试创建新承诺时会抛出 NPE,从而导致 500 错误。我的建议是简单地删除这个函数调用。