Search code examples
node.jsazure-cosmosdbcosmosclient

How to execute the cosmos db stored procedure in nodejs?


I am trying to execute the stored procedure in nodejs. I am using cosmosclient and when I tried executing the code but I'm not able get the response or data back. Here is my piece of code.

private async executeSprocInternal(sprocName, sprocParams) {
        try {
            var sprocLink = this.sprocsUrl + '/' + sprocName;  //sprocLink:: dbs/testDB/colls/test-container/sprocs/test
            var _stdate;
            var _partition:any ={} ;
            if (conf.partition == true)   _partition.partitionKey = 'id';
           
            if (this.showLog == true) _stdate = new Date();
            return new Promise((resolve, reject) => { 
                this.container.scripts.storedProcedure(sprocName).execute(sprocLink,sprocParams,_partition).then((results) =>{
                    if (this.showLog == true) {
                        console.log('Completed Proc ', sprocName, ' in ', ((new Date()).getTime() - _stdate.getTime()), ' msec');
                        }
                    resolve(results);
                })
                .catch((error) =>{
                    console.log("exequeryerror")
                    reject(error)
                })
            });

        } catch (e) {
            console.log(2,e)
            return new Promise((resolve, reject) => {
                 reject(e);
             });
        }
    }

Thanks for the help.


Solution

  • According to API documentation,sprocLink shouldn't be passed.

    function execute(partitionKey: any, params?: any[], options?: RequestOptions)

    Also, you need to pass value of your Partition Key.(I guess 'id' is your partition key.)

    I have tried this and it can work fine, you can have a try:

        async function executeSprocInternal(sprocName, sprocParams) {
            try {
        
                var sprocLink = 'dbs/Test/colls/data/sprocs' + '/' + sprocName;//sprocLink:: dbs/testDB/colls/test-container/sprocs/test
                var _stdate = new Date();;
                var partitionKey  = 'fruit';
            
                return new Promise((resolve, reject) => {
                    container.scripts.storedProcedure(sprocName).execute(partitionKey,sprocParams).then((results) => {
                        console.log('Completed Proc ', sprocName, ' in ', ((new Date()).getTime() - _stdate.getTime()), ' msec');
                        resolve(results);
                    })
                        .catch((error) => {
                            console.log("exequeryerror")
                            reject(error)
                        })
                });
            } catch (e) {
                console.log(2, e)
                return new Promise((resolve, reject) => {
                    reject(e);
                });
            }
        }
    
        executeSprocInternal("getData", "success").then((message) => {console.log(message);})
    

    Hope this can help you.


    Update

    Here is my sample:

    enter image description here

    As the screenshot shows,/category is my Partition key path, fruit is my Partition key value. You just need to pass "fruit" to execute method, like this:

    container.scripts.storedProcedure(sprocName).execute("fruit",sprocParams)