Javascript Azure Functions中未提取或显示输出绑定

时间:2017-11-08 00:18:44

标签: javascript azure azure-cosmosdb azure-functions

我在使用简单的Azure功能将JSON有效负载转储到CosmosDB时遇到了一些困难。这是所需的设置:

[HTTP TRIGGER] -> [JS FUNCTION] -> [INSERT JSON TO COSMOS DB]

我在网络控制台中通过集成添加了cosmosDB输出绑定

enter image description here

我知道这个部分有效,因为它会在运行时自动创建集合。

然而,这是我的成功结束的地方。

我在这里包含了我的代码。也许有一些我错过的明显事实:

function.json

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "documentDB",
      "name": "outputDocument",
      "databaseName": "outDatabase",
      "collectionName": "MyCollection",
      "createIfNotExists": true,
      "connection": "mydocdb_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}

index.js

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    context.bindings.outputDocument = {test: "hello world"}
    context.done();
};

context.log(上下文)的片段输出 我也偷看了上下文变量,看起来外包绑定不存在。我原本希望在这里看到outputDocument:

bindings: 
   { req: 
      { originalUrl: 'https://log-ugly-url-here',
        method: 'POST',
        query: [Object],
        headers: [Object],
        body: [Object],
        params: {},
        rawBody: '{\n    "name": "Azure"\n}' } },
  bind: [Function],

在门户网站中执行此功能可获得200 OK,但我的cosmosdb集合中没有显示任何值。

任何地方都不会显示错误。

任何人都可以看到我可能错过的内容吗?

更新 我建议每个this document尝试在有效负载周围使用JSON.stringify():

context.bindings.outputDocument = JSON.stringify({test: "hello world"});

但是,这还没有解决问题。我注意到绑定配置部分下的文档不推荐这种策略。

更新 - 2017年11月8日(上午10:12)

我在azure中使用Data Explorer刀片来查看我的集合中是否有结果。我决定使用Robo 3T Mongo客户端。现在,当我查询集合时,我收到以下错误:

Error: error: {
    "_t" : "OKMongoResponse",
    "ok" : 0,
    "code" : 1,
    "errmsg" : "Unknown server error occurred when processing this request.",
    "$err" : "Unknown server error occurred when processing this request."
}

我试着查看这个cosmosdb的日志,但没有出现错误。

1 个答案:

答案 0 :(得分:1)

我也碰到了这个。在我看到这个之前,一直砰砰直跳:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-integrate-store-unstructured-data-cosmosdb

“目前,Azure Cosmos数据库触发器,输入绑定和输出绑定仅适用于SQL API和图谱API帐户”

看起来Azure中存在一个意外破坏数据库的错误。