我正在使用 Azure 函数做一些工作,一切都很好,只是我无法从结果中获取响应正文:
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const fetch = require('node-fetch');
const myURL= (req.query.apiURL|| (req.body && req.body.apiURL));
fetch(myURL)
.then(data => {
if (!data.ok) {
throw new Error('some error occurred');
}
return data;
})
.then(data => data.text())
.then(text =>
context.res = {
body: text //here is the problem
});
}
function.json
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
使固定
空响应与使用async
方法有关,await
因此只需删除异步或将 await 与异步一起使用。