I am a beginner in Typescript and CosmosDB, and I have been searching online all day for a solution to my problem. Unfortunately, I haven't been able to find one that works for my specific case. I hope someone here can help me out.
I have been trying to create RESTful APIs using Azure Functions (JavaScript/Typescript) to connect to and query data from my Azure Cosmos DB. I used data binding to link the database with my functions. However, I'm unsure how to retrieve values from the database based on specific column criteria (e.g., select id from table where username="abc").
For clarification, here is an example of a user item in my Cosmos DB:
{
"id": "1",
"userid": "33218898",
"username": "test1",
"password": "psw",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bc8dfcc8d9cfc892dfd3d1">[email protected]</a>",
"momentID": [
"1",
"2",
"3"
],
"followingID": [
"2",
"3"
],
"followerID": [
"2",
"3"
]
}
This is how I fetch users from the Azure Function:
module.exports = async function (context, req, inputDocument) {
context.log('JavaScript HTTP trigger function processed a request.');
if (!!inputDocument && inputDocument.length > 0) {
if(req.query.id || (req.body && req.body.id)){
context.res = {
// status: 200, /* Defaults to 200 */
body: "User with id [" + req.query.id + "] is: " + inputDocument[req.query.id-1].username
};
}
else if(req.query.name || (req.body && req.body.name)){
const user = inputDocument.select("[@username='test1_update']");
const userid = user.id;
context.log('User id with username:', userid);
}else{
var arr_user = new Array(inputDocument.length);
for(var _i = 0; _i <inputDocument.length; _i++)
{
arr_user[_i] = inputDocument[_i].username;
}
context.res = {
status: 200,
body: "Hello " + arr_user
};
context.log('Username:', arr_user);
}
}
else{
context.log('Nothing in the inputDocument');
}
};
It seems like the `select` function is not defined in Typescript. Can anyone help me with this issue?