【发布时间】:2019-09-14 08:24:29
【问题描述】:
我有一个场景,事件中心正在接收数据并且触发 Azure 函数来获取数据,Azure 函数应该使用获取的数据作为有效负载发出 POST 请求并从服务器获取响应。我正在使用基于 Promise 的 npm Http 模块“Axios”,因为响应时间可能因负载而异。 (Azure 函数在 NodeJS 中);问题是,没有 Post 请求通过
尝试过异步 - 等待 Post 请求的调用,仍然无法正常工作。
这里是代码
module.exports = async function (context, eventHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array ${eventHubMessages}`);
var data = {};
eventHubMessages.forEach((message, index) => {
context.log(`Processed message ${message}`);
data = message;
});
const axios = require('axios');
module.exports=async function(context,req){
let response= await axios.post('http:example.com/post',
{
data
}
)
.then((res) => {
context.log(`statusCode: ${res.statusCode}`)
context.log(res)
})
.catch((error) => {
context.error(error)
})
context.log(`Output content: ${res}`);
}
};
这里是function.json
{
"bindings": [
{
"type": "eventHubTrigger",
"name": "eventHubMessages",
"direction": "in",
"eventHubName": "name-eventhub",
"connection": "eventhub-key",
"cardinality": "many",
"consumerGroup": "$Default"
},
{
"type": "http",
"name": "$return",
"direction": "out"
}
]
}
预期输出:
Processed message [object]
statusCode : 200
Output content: response // print response in here
实际输出:
Processed message [object]
输出中没有显示状态代码或响应。
【问题讨论】:
-
这里还有一个问题,你的 module.exports 被分配了两次。你只想这样做一次。我知道 lambda,而不是 azure 函数,所以我不确定函数签名到底如何,但你绝对不想要两个 module.exports
标签: node.js http-post axios azure-functions azure-eventhub