【问题标题】:How to return the result of an Azure Function如何返回 Azure 函数的结果
【发布时间】:2018-02-09 05:48:20
【问题描述】:

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

【问题讨论】:

  • 我没有使用 Node.js 制作任何函数,但也许您可以将 context.res = ... 和 context.done(); 移动到您记录 cmets 的回调函数中?
  • 我试过了,结果是一样的

标签: javascript node.js azure azure-functions


【解决方案1】:

移动分配context.res 并调用context.done 以承诺回调。在标题中将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();
}

【讨论】:

  • 我试过了。 fetchComments(req.body.videoId) .fork(e => context.log('ERROR', e), p => { context.log('cmets', p.cmets); context.res = { body: p .cmets }; context.done(); }) 但结果是一样的。我只去状态 200 好的。没有正文内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-21
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多