0

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.

virat
  • 45
  • 1
  • 6
  • "I tried executing the code but I'm not able get the response or data back" - What do you mean by that? Did you debug the above code to check what is inside the `then` block after the stored proc `execute`? – krishg Sep 24 '20 at 08:51
  • Yes. results are coming undefined. – virat Sep 24 '20 at 09:31

1 Answers1

2

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)
Steve Johnson
  • 8,057
  • 1
  • 6
  • 17
  • I tried with this previously but it gave me error stating `details: { Error: Partition key [{"success":"OK","norecords":"NONE","failed":"FAIL","params":{"id":"abc","curr_time":1601025728307,"seldate":"20200925","selmonth":202009}}] is invalid.` – virat Sep 25 '20 at 09:28
  • @virat Sorry for late reply. According to the description of error, you pass a wrong Partition key value. Please refer to my update, and check for that. – Steve Johnson Sep 28 '20 at 01:51