首页 > 解决方案 > 如何让 cosmosdb 触发一个 azure 函数并调用另一个 http 函数

问题描述

我需要监听 azure cosmos db 中的变化,然后根据收到的信息对另一个 api 进行 POST 调用。

我添加了这个function.json

{
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "name": "input",
      "direction": "in",
      "leaseCollectionName": "leases",
      "connectionStringSetting": "validAppsTrigger2_ConnectionString",
      "databaseName": "dev",
      "collectionName": "validApps",
      "createLeaseCollectionIfNotExists": true
    },
    {
      "name": "response",
      "direction": "out",
      "type": "http"
    }
  ]
}

这是我的 index.js

module.exports = function (context, input) {
    context.log('Document Id: ', input[0].id);
    // should I call http manually here?
    context.done();
};

但我不确定我应该如何从那里调用另一个 azure http 函数,

标签: node.jsazureazure-functionsazure-cosmosdb

解决方案


我需要一个外绑定吗?

不,HTTP 输出绑定需要 HTTP 触发器。您正在使用 Cosmos 触发器,因此无法执行此操作。参考:https ://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-output 。

使用 HTTP 输出绑定来响应 HTTP 请求发送者。此绑定需要 HTTP 触发器,并允许您自定义与触发器请求关联的响应。

我应该在上述函数中进行常规的 http 调用吗?

是的,你可以像@Thiago Custodio 所说的那样在你的函数中进行常规的 http 调用。


推荐阅读