Azure Function Responding before Query is Complete.

Serge Nalishiwa 31 Reputation points
2021-03-12T12:35:08.633+00:00

Following this [doc][1]

[1]: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=javascript with aim of Queue trigger, get multiple docs, using SqlQuery.

My function (index) is as follows:

module.exports = function (context, req) {  
      
    var performances = context.bindings.performances;  
  
    context.log("The list is this long : " + performances.length);  
  
    context.log("The requested course code is : " + (req.query.CourseID || (req.body && req.body.CourseID)));  
  
    if (performances.length !== 0) {  
        context.res = {  
            body: JSON.stringify(performances),   
            headers: {  
                'Content-Type': 'application/json'  
            }  
        };  
    }  
    else {  
  
        context.res = {  
            status: 404,  
            body: "No Performance found",  
            headers: {  
                'Content-Type': 'application/json'  
            }  
        };  
    }  
  
    context.done();  
};  
  

and my function.json is as follows:

{  
  "bindings": [  
    {  
      "authLevel": "function",  
      "type": "httpTrigger",  
      "direction": "in",  
      "name": "req",  
      "methods": [  
        "get",  
        "post"  
      ]  
    },  
    {  
      "type": "http",  
      "direction": "out",  
      "name": "res"  
    },  
    {  
      "type": "cosmosDB",  
      "direction": "in",  
      "name": "performances",  
      "databaseName": "Core",  
      "collectionName": "StudentPerformance",  
      "connectionStringSetting": "CosmoConnectionString",  
      "partitionKey": "/UserID",  
      "sqlQuery": "SELECT * from c where c.CourseID = {CourseID}"  
    }  
  ]  
}  
  1. I attempted to query Cosmos DB from the Microsoft Azure Storage Explorer and got a 200. Which implies my query was OK.
  2. I then attempted to use Azure Function with Cosmos input Binding. I got the results I wanted.
  3. Attempting to make the query dynamic has proved to be a challenge. Any Ideas?

I keep getting an empty list. 😭

CourseID is passed to the query I think so without explicitly specifying it?

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

Accepted answer
  1. ChaitanyaNaykodi-MSFT 24,666 Reputation points Microsoft Employee
    2021-03-19T03:28:28.76+00:00

    Hello @Serge Nalishiwa , I think the issue is caused as your CourseID is of type INT, But in the query it is being passed as a string. Can you please modify the sqlQuery in your function.json as shown below.

    "sqlQuery": "SELECT * from c where c.CourseID = StringToNumber({CourseID})"

    It worked for me when I tried it out.

    79400-image.png

    Additionally you can go through this documentation if you wish run more dynamic queries.
    Regarding how "CourseID" is passed on to your output you can go through this documentation to understand more about bindings expressions and patterns. This documentation explains more about the properties for HTTP triggered functions.
    Please let me know if there are any additional concerns. Thank you!

    0 comments No comments

0 additional answers

Sort by: Most helpful