Estoy usando una función de Azure para trabajar, todo está bien, excepto que no puedo obtener el cuerpo de respuesta del resultado:
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"
}
]
}
La respuesta vacía estaba relacionada con el uso de un async
método sin, await
por lo que simplemente elimine el async o use await con async.
Async / await mejorado, versión de @BowmanZhu
const fetch = require('node-fetch');
module.exports = async function (context, req) {
try{
context.log('JavaScript HTTP trigger function processed a request.');
const myURL= (req.query.apiURL || (req.body && req.body.apiURL)),
fetchResp = await fetch(myURL),
resBody =fetchResp.text();
/** TODO LOGIC **/
context.res = {
status:200,
body: resBody
};
}catch(err){
context.res = {
status:500,
body: err.message
};
}
}
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras