Azure Function Cosmos DB Output Binding issue

Matt Heagerty 46 Reputation points
2020-09-23T15:13:10.407+00:00

I have a javascript Azure function with an IoTHub trigger and a CosmosDB output. This function decodes the IoTHubMesages and creates multiple JSON objects to be sent to CosmosDB. Shown below is the structure of my function with pieces taken out for clarity and conciseness. In addition, this function uses an HTTP trigger but the same overall structure as my actual function.

module.exports = async function (context, req) {

context.bindings.outputDocument = [];

count= [1, 2, 3, 4, 5];

count.forEach(processSingleRecord);

function processSingleRecord(record) {

        var item = JSON.stringify({
            "tag" : 'Count',
            "value" : record
        }); // end JSON object

    // Set the output binding data from the item
    context.bindings.outputDocument.push(item);

};

context.done();
};

Upon running, I receive an error saying "...Value cannot be null. (Parameter 'document')". I am confused by why this code structure does not work and would appreciate any insight.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,303 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,449 questions
{count} votes

Accepted answer
  1. JayaC-MSFT 5,526 Reputation points
    2020-09-30T15:54:26.707+00:00

    @Matt Heagerty Thank you for sharing the details offline. I will share the sample codes here.
    e.g. 1( input JSON payload):

    index.js

    module.exports = async function (context, req) {  
       
    context.bindings.outputDocument =[];  
       
    for(let i = 0, len=req.body.length; i<len;i++){  
            const doc = req.body[i];  
            context.bindings.outputDocument.push(doc);  
        }  
        OR  
       
    req.body.forEach(message =>{  
            const doc = message;  
            context.bindings.outputDocument.push(doc);  
        });  
       
       
    context.done();  
       
    };  
    

    sample payload:

    [  
        {  
            "id":"1",  
            "name": "Jaya",  
            "surname": "Chatterjee"  
        },  
        {  
            "id":"2",  
            "name": "Matt",  
            "surname": "Heagerty"  
        }  
    ]  
    

    In this particular scenario, we have tested with a similar structure :

    module.exports = async function (context, req) {  
    context.log('JavaScript HTTP trigger function processed a request.');  
        
    context.bindings.outputDocument = JSON.stringify([{  
    id: "record_3",  
    tag: "Count",  
    value: "some value 3"  
    }, {  
    id: "record_4",  
    tag: "Count",  
    value: "some value 4"  
    }, {  
    id: "record_5",  
    tag: "Count",  
    value: "some value 5"  
    }, {  
    id: "record_6",  
    tag: "Count",  
    value: "some value 6"  
    }]);  
        
    context.done();  
    }  
    

    It works fine. So we would suggest you to form the objects first and then run the stringify function once , not in a loop.

    If you think this helped, please "accept the answer" and " up-vote" so that this could help others in the community.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful