gpt4 book ai didi

node.js - 如何将 AWS SDK 与 Promise 结合使用

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:14 25 4
gpt4 key购买 nike

首先,我使用简化回调方法工作得很好。现在,我想使用 Promise 来查询 AWS dynamoDB。 我指this .

但总是500内部服务器错误。我使用 lambda 和 node.js 4.3。我错过了什么吗?

处理程序.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 在尝试创建新的 Promise 时会抛出 NPE,从而导致 500 错误。我的建议是简单地删除这个函数调用。

关于node.js - 如何将 AWS SDK 与 Promise 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38847166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com