question

SergeNalishiwa avatar image
0 Votes"
SergeNalishiwa asked ChaitanyaNaykodiMSFT-9638 answered

Azure Function Responding before Query is Complete.

Following this doc



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-functionsazure-cosmos-db
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @SergeNalishiwa, Thank you for reaching out. I am currently working on the issue faced, I will share my updates soon.

1 Vote 1 ·

Hello @SergeNalishiwa, my sincere apologies for the delay. I tried the code given above and it worked for me. I was able to get the Documents with the CourseID parameter. I passed the CourseID in following format.
1. Query parameter : http://localhost:7071/api/HttpTrigger1?CourseID=3
2. Body: Using postman in format below.

78947-image.png

Can you please confirm if you were passing on the CourseID in similar way? Please do let me know if the issue still persist, I will be glad to continue with our discussion. Thank you


0 Votes 0 ·
image.png (35.3 KiB)
SergeNalishiwa avatar image SergeNalishiwa ChaitanyaNaykodiMSFT-9638 ·

@ChaitanyaNaykodiMSFT-9638

I tried the parameters:

79182-short2.jpg

then the body with and without the parameters:
79123-short1.jpg


and the removed the where statement just to make sure the connection was ok:
79124-short3.jpg

It still didn't work. How is CourseID even passed to the query secretly? It seems like magic by the way....

The Function Runtime Version is : 3.0.15371.0

Cosmos DB: Tried the emulator, the serverless and provisioned. Got the same result: No performances found.



0 Votes 0 ·
short2.jpg (47.7 KiB)
short1.jpg (50.7 KiB)
short3.jpg (117.9 KiB)

1 Answer

ChaitanyaNaykodiMSFT-9638 avatar image
0 Votes"
ChaitanyaNaykodiMSFT-9638 answered

Hello @SergeNalishiwa, 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!



image.png (123.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.