Hello @Nischal Mainali
It seems like the container 'reg-file-upload' in the database 'file-upload-transaction' does not exist.
To automatically create the container, you can set 'createIfNotExists' to 'true'.
You can try the following code snippet to create a new document in Cosmos DB using an output binding:
import { AzureFunction, Context } from "@azure/functions";
import { CosmosClient } from "@azure/cosmos";
const endpoint = process.env.CosmosDbEndpoint;
const key = process.env.CosmosDbKey;
const client = new CosmosClient({ endpoint, key });
const databaseName = "file-upload-transaction";
const containerName = "reg-file-upload";
const cosmosTriggerCheck:
AzureFunction = async function (context: Context, req: any): Promise {
const name = req.query.name;
const description = req.query.description;
context.bindings.outputDocument = {
name: name, description: description
};
context.res = { body: `The api is up and running, ${name}!` };
};
export default cosmosTriggerCheck;
In the above code snippet, we are using the CosmosClient
class from the @azure/cosmos
package to create a new document in Cosmos DB. We are also using an output binding to write the document to Cosmos DB. The outputDocument
binding is declared in the function.json
file as follows:
{
"name": "outputDocument",
"type": "cosmosDB",
"databaseName": "file-upload-transaction",
"collectionName": "reg-file-upload",
"createIfNotExists": true,
"connectionStringSetting": "CosmosDbConnectionSetting",
"direction": "out"
}
Make sure to replace the CosmosDbConnectionSetting
with the name of your Cosmos DB connection string setting in the local.settings.json
file. Also, make sure to replace the databaseName
and collectionName
with the names of your database and collection respectively. Let me know if this helps!
I hope that this response has addressed your query and helped you overcome your challenges. If so, please mark this response as Answered. This will not only acknowledge our efforts, but also assist other community members who may be looking for similar solutions.