如何返回JSON Azure functio应用程序httptrigger?

问题描述

我使用http触发器创建了Azure函数应用,该应用应返回JSON文件。

这是我的nodejs脚本的一部分。


var promise = Promise.all([getFood("first"),getFood("second"),getFood("third")]); 
promise.then(function(data) {
        let res = JSON.stringify(data);

        context.res = {
            body: res
        };
        context.done();
});

它什么也不返回。

但是当我尝试使用类似这样的脚本时,它会起作用:

var promise = Promise.all([getFood("first"),getFood("third")]); 
promise.then(function(data) {
        let res = JSON.stringify(data);


});

context.res = {
     body:"This is text"
};
context.done();

它将在正文中返回字符串。

解决方法

尝试一下:

module.exports = async function (context,req) {
   var data = await Promise.all([getFood("first"),getFood("second"),getFood("third")]); 

   return {
     body: JSON.stringify(data)
   };
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...