Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services AWS API网关&x2B;Lamda(非异步)和#x2B;获取(异步)_Amazon Web Services_Aws Lambda_Aws Api Gateway - Fatal编程技术网

Amazon web services AWS API网关&x2B;Lamda(非异步)和#x2B;获取(异步)

Amazon web services AWS API网关&x2B;Lamda(非异步)和#x2B;获取(异步),amazon-web-services,aws-lambda,aws-api-gateway,Amazon Web Services,Aws Lambda,Aws Api Gateway,我有一个api网关,需要与lambda集成,以改变一些请求数据,然后将其传递给另一个服务 我已经同步设置了lambda(事件、上下文、回调),并且可以很好地处理同步请求。然而,当我向lambda添加“代理”功能时(向另一个端点发出请求,获取响应并将所述响应返回给api网关),我似乎无法正确处理承诺 以下是我的一些伪代码: const { fetch } = require('node-fetch'); const proxyMethod = (event, callback) => {

我有一个api网关,需要与lambda集成,以改变一些请求数据,然后将其传递给另一个服务

我已经同步设置了lambda(事件、上下文、回调),并且可以很好地处理同步请求。然而,当我向lambda添加“代理”功能时(向另一个端点发出请求,获取响应并将所述响应返回给api网关),我似乎无法正确处理承诺

以下是我的一些伪代码:

const { fetch } = require('node-fetch');
const proxyMethod = (event, callback) => {
  const request = // valid fetch request
  fetch(request)
    .then(result => callback(null, result))
    .catch(error => callback(Error(error));
}
exports.handler = (event, context, callback) => {
  proxyMethod(event, callback);
}
问题是我认为我没有同步处理承诺,在“proxyMethod”中使用async/await基本上没有效果,或者回调没有等待承诺得到解决

这是我在API网关日志中得到的:

Sending request to https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:########:function:#########/invocations

Received response. Status: 200, Integration latency: 749 ms

Endpoint response headers: {Date=Tue, 21 Jul 2020 02:11:31 GMT, Content-Type=application/json, Content-Length=4, Connection=keep-alive, x-amzn-RequestId=######, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=########;sampled=0}

Endpoint response body before transformations: null

Execution failed due to configuration error: Malformed Lambda proxy response

Gateway response type: DEFAULT_5XX with status code: 502

在这里找到了有用的信息。在许多不同的地方,节点运行时都支持使用异步流,因此我需要使用wait来等待获取请求,然后在“response.json()”中再次等待以从服务下游获取响应