azure - 从异步的 azure 函数返回什么

标签 azure azure-functions

我有一个 azure 的函数,看起来有点像这样:

module.exports = async (context: Context, req: any) => {
  const canProceed = await new Auth().verify(context, req);

  if (!canProceed) {
    context.res = {
      status: 401,
      body: 'Unauthorised'
    };

    return context.done(undefined, context);
  }

  context.res = doStuff();

  return context.done(undefined, context);
}

当我在本地运行该函数时,我收到此警告或错误:

Error: Choose either to return a promise or call 'done'. Do not use both in your script.

文档中尚不清楚这应该如何工作

最佳答案

看看 doc .

When using the async function declaration or plain JavaScript Promises in version 2.x of the Functions runtime, you do not need to explicitly call the context.done callback to signal that your function has completed. Your function completes when the exported async function/Promise completes.

When exporting an async function, you can also configure an output binding to take the return value. This is recommended if you only have one output binding.

如果不是,请将 function.json 中的 name 属性更改为 $return

对于http触发器,

{
  "type": "http",
  "direction": "out",
  "name": "$return"
}

你的函数代码也需要修改。请注意,doStuff() 应该按预期返回有效响应。

module.exports = async (context: Context, req: any) => {
  const canProceed = await new Auth().verify(context, req);

  if (!canProceed) {
    return {
      status: 401,
      body: 'Unauthorised'
    };
  }
  else return doStuff();

}

关于azure - 从异步的 azure 函数返回什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54630039/

相关文章:

azure - Azure Functions 的托管标识可以跨多个订阅进行访问吗?

c# - 是否可以监听具有多个azure功能的服务总线?

Azure Functions 未显示在 Function App 中

azure - 控制对 Azure 上 Web 应用程序的访问

Azure:禁用 .ppk 文件进行 ssh 连接

cordova - 如何从 Cordova Azure 验证应用程序查询 Azure Web API

asp.net-mvc - MVC 4 移动检测 View 与布局不同

azure - 我在哪里可以获得 Microsoft 管理的 key ?

c# - 禁用 Azure Insights 采样不起作用

c# - Azure 函数创建 blob