0
votes

I'm having a bit of difficulty getting my simple Azure Function to dump a JSON payload into a CosmosDB. Here is the desired setup:

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

I've added a cosmosDB output binding via Integrate in the web console

enter image description here

I know this part works because it will auto-create the collection upon running.

However, this is where my success ends.

I've included my code here. Maybe there is something obvious that I have missed:

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();
};

snippet output of context.log(context) I've also peeked into the context variables, and it seems that the outbund bindings not there. I would have expected to see outputDocument here:

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

Executing this function in the portal gets a 200 OK, but no values show up in my cosmosdb collections.

No errors are shown anywhere.

Can anyone see what I might have missed?

Updates It was recommended that I try to use JSON.stringify() around the payload, per this document:

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

However, this has not resolved the issue. I've noticed that the documentation under the binding config sections does not recommend this strategy.

Update - Nov. 8, 2017 (10:12 AM)

I was using the Data Explorer blade in azure to see if there were results in my collections. I decided to use Robo 3T Mongo client. Now, when I query the collection I get the following error:

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."
}

I tried to look at the logs for this cosmosdb, but no errors show up.

1
Based on the example in this doc it appears the DocDB binding from javascript expects a string (in a JSON.stringify()). Let me know if that works. doc herejeffhollan
Thanks for the input. Unfortunately that did not work either. I'm updating the original question with different strategies until we get this solved.Sam Texas
Your initial code works just fine for me, I got the "hello world" document inserted. context.log(context) is the same, so it's not a problem. Just in case: the document gets auto-generated ID, maybe you just don't see it?Mikhail Shilkov
@Mikhail thanks for trying it out. I've updated the question again with output from my mongo client (Robo 3T).Sam Texas

1 Answers

2
votes

I ran into this as well. Kept banging my head until I saw this:

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

“At this time, the Azure Cosmos DB trigger, input bindings, and output bindings work with SQL API and Graph API accounts only“

Looks like there is a bug in Azure that accidentally corrupts the database.