我从 Azure Functions 开始。我有以下代码:
module.exports = function (context, req)
{
context.log('JavaScript HTTP trigger function processed a request.');
context.log(context.req.body.videoId)
if (context.req.body.videoId =! null)
{
context.log('inicia a obtener comentarios')
const fetchComments = require('youtube-comments-task')
fetchComments(req.body.videoId)
.fork(e => context.log('ERROR', e), p => {
context.log('comments', p.comments)
})
context.res = { body: fetchComments.comments }
}
else {
context.res = {
status: 400,
body: "Please pass a videoId on the query string or in the request body"
};
}
context.done();
};
如何返回 fetchComments 返回的 JSON?
最佳答案
移动分配context.res
并调用context.done
以 promise 回调。在 header 中将 Content-Type
设置为 application/json
。根据您的代码,类似于
if (context.req.body.videoId =! null) {
context.log('inicia a obtener comentarios')
const fetchComments = require('youtube-comments-task')
fetchComments(req.body.videoId)
.fork(e => context.log('ERROR', e), p => {
context.log('comments', p.comments);
context.res = {
headers: { 'Content-Type': 'application/json' },
body: p.comments
};
context.done();
});
}
else {
context.res = {
status: 400,
body: "Please pass a videoId on the query string or in the request body"
};
context.done();
}
关于javascript - 如何返回 Azure 函数的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48693015/