如何返回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)
   };
}