javascript - 如何返回 Azure 函数的结果

标签 javascript node.js azure azure-functions

我从 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/

相关文章:

node.js - 如何让 Gulp 识别编译文件中的更改/更新?

azure - 在 MS Azure 上部署 Acumatica ERP

javascript - Jquery 循环问题

javascript - 工厂函数 : why doesn't status variable change to "OK"?

node.js - 如何使用 sinon stub require ('firebase-admin' ).auth().getUserByEmail() ?

azure - Application Insights 中的用户流和 channel 功能之间的差异

c# - Azure 函数 SDK

javascript - 为什么 javascript 不能在 if 语句中使用 .style.color

javascript - 附加时编辑结果?

node.js - Nodejs 代理请求合并