Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

javascript - 在异步AWS Lambda函数中使用带有节点获取模块的node.js时遇到问题(trouble using node.js w/ node-fetch module in async AWS Lambda Function)

Here's what I'm trying to do: I have a lambda function triggered by a webhook that will process that data and use it to make a POST request to an api.(这是我要执行的操作:我有一个由Webhook触发的lambda函数,该函数将处理该数据并将其用于向api发出POST请求。)

I'm using Node.js 12 and the node-fetch module.(我正在使用Node.js 12和node-fetch模块。) Right now the function is being triggered correctly, but it won't send the POST the first time it is called.(现在,该函数已正确触发,但是在第一次调用时不会发送POST。) But if I trigger the lambda function repeatedly in a short amount of time, the requests after the first will get through.(但是,如果我在短时间内重复触发lambda函数,则第一个请求之后的请求将通过。) Here's the code in my lambda function's index.js file:(这是我的lambda函数的index.js文件中的代码:) const fetch = require('node-fetch'); exports.handler = async (event) => { sendPost(); const response = { statusCode: 200, body: "Ok", }; return response; }; function sendPost() { const url = "http://requestbin.net/r/vjv4mvvj"; const body = { foo: "foo", bar: "bar", baz: "baz" }; const params = { method: "POST", mode: "cors", headers: {"Content-Type":"application/json"}, body: JSON.stringify(body) }; fetch(url, params); } When I delete the async in front of (event) , then the request goes through the first time, but the server that sends the webhook in the first place doesn't get a response.(当我删除(event)前面的async时,请求将首次执行,但是首先发送webhook的服务器没有得到响应。) So basically I need the POST to send every time the function is called and also be able to return the 200 status code.(因此,基本上我需要在每次调用该函数时发送POST,并且还能够返回200状态代码。) I don't have much experience working with async so I know it's related to that, but I'm scratching my head.(我没有使用异步的丰富经验,所以我知道它与此相关,但是我挠头了。)   ask by benjfriedrich translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The fetch returns a promise, you need an await .(提取返回承诺,您需要await 。)

Also you need to mark the sendPost function as async .(另外,您需要将sendPost函数标记为async 。) You also need the handler function to wait for sendPost function to finish.(您还需要处理程序函数来等待sendPost函数完成。) const fetch = require('node-fetch'); exports.handler = async (event) => { await sendPost(); const response = { statusCode: 200, body: "Ok", }; return response; }; async function sendPost() { const url = "http://requestbin.net/r/vjv4mvvj"; const body = { foo: "foo", bar: "bar", baz: "baz" }; const params = { method: "POST", mode: "cors", headers: {"Content-Type":"application/json"}, body: JSON.stringify(body) }; await fetch(url, params); }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...